google-api-php-client connect with key.p12

google-api-php-client connect with key.p12

我可以使用 API 密钥和 OAuth 2.0 客户端 ID 将事件添加到我的 google 日历,但我想在没有 google 授权屏幕的情况下执行此操作。

我按照在这个 post 上找到的信息:

计算器。com/questions/8995451/how-do-i-connect-to-the-google-calendar-api-without-the-oauth-authentication

为此,我创建了一个 'Service account keys'。我去了我的项目凭证和 select 'Create Credentials > Service Account Key'

然后:

服务帐号 > my_project_name

密钥类型 > p12

我保存了密钥文件key.p12

这是我的代码:

<?php
// display error, debbug
ini_set('display_errors',1);

session_start();

require_once './google-api-php-client/src/Google_Client.php';
require_once './google-api-php-client/src/contrib/Google_CalendarService.php';

// following values are taken from : console.developers.google.com/apis/credentials?project=projectname credentials
// OAuth 2.0 client IDs : Client ID
const CLIENT_ID = 'xxxx';
// Service account keys : Service account
const SERVICE_ACCOUNT_NAME = 'my_project_name';

// key 
const KEY_FILE = './key.p12';

$client = new Google_Client();
$client->setApplicationName("Google Calendar API Quickstart");

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}

// Load the key in PKCS 12 format
$key = file_get_contents(KEY_FILE);

$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/calendar'),
    $key)
);

$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);

//Save token in session
if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
}

// my code here
$event = new Google_Event(...
$calendarId = 'mycalendarID@group.calendar.google.com';
$event = $cal->events->insert($calendarId, $event);

?>

这是错误消息:

Fatal error:  Uncaught Google_AuthException: Error refreshing the OAuth2 token, message: '{
  'error' : 'invalid_client',
  'error_description' : 'The OAuth client was not found.'
}' in /home/www/google-api-php-client/src/auth/Google_OAuth2.php:288
Stack trace:
#0 /home/www/google-api-php-client/src/auth/Google_OAuth2.php(264): Google_OAuth2->refreshTokenRequest(Array)
#1 /home/www/google-api-php-client/src/auth/Google_OAuth2.php(218): Google_OAuth2->refreshTokenWithAssertion()
#2 /home/www/google-api-php-client/src/service/Google_ServiceResource.php(167): Google_OAuth2->sign(Object(Google_HttpRequest))
#3 /home/www/google-api-php-client/src/contrib/Google_CalendarService.php(469): Google_ServiceResource->__call('insert', Array)
#4 /home/www/goog in /home/www/google-api-php-client/src/auth/Google_OAuth2.php on line 288

$client->getAccessToken() 似乎没有设置,但我不知道为什么。

在此先感谢您的帮助。

创建服务帐户密钥并下载包含私钥的 *.json 文件。将刚刚下载的 *.json 文件放在您选择的目录中(示例中的 credentials.json 文件)。然后转到 Google 日历-> 共享并在列表中添加服务帐户 ID。

putenv( 'GOOGLE_APPLICATION_CREDENTIALS=credentials.json' );

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope( 'https://www.googleapis.com/auth/calendar' );
$client->setHttpClient( new GuzzleHttp\Client( [ 'verify' => false ] ) );   // disable ssl if necessary

$service = new Google_Service_Calendar( $client );  

// list events
$service->events->listEvents( $calendar_id, array( 'timeMin' => ( new DateTime )->format( DateTime::RFC3339 ) ) );

// add event
$event = new Google_Service_Calendar_Event( $data );
$event = $service->events->insert( $calendar_id, $event );

您可以在 GitHub 的 releases 页面中找到带有自动加载器的 Google API PHP 客户端库包,因此唯一需要的 require_once 调用用于自动加载文件:

require_once '/path/to/google-api-php-client/vendor/autoload.php';

而且,Google docs 可能会进一步帮助您,这只是对我有用的一种方式,因此它可能对您也有用。