简体   繁体   English

google-api-php-client与key.p12连接

[英]google-api-php-client connect with key.p12

I'm able to add event to my google calendar with an API keys and OAuth 2.0 client IDs but I want to do this without the authorization screen of google. 我可以使用API​​密钥和OAuth 2.0客户端ID将事件添加到我的Google日历中,但是我想在没有Google授权屏幕的情况下执行此操作。

I followed the information found on this post: 我关注了这篇文章中的信息:

stackoverflow.com/questions/8995451/how-do-i-connect-to-the-google-calendar-api-without-the-oauth-authentication stackoverflow.com/questions/8995451/how-do-i-connect-to-the-google-calendar-api-without-the-oauth-authentication

For this, I created a 'Service account keys'. 为此,我创建了一个“服务帐户密钥”。 I went to my project credentials and select 'Create Credentials > Service Account Key' 我转到了我的项目凭据,然后选择“创建凭据>服务帐户密钥”

Then: 然后:

service account > my_project_name 服务帐户> my_project_name

Key type > p12 密钥类型> p12

I saved the key file key.p12 我保存了密钥文件key.p12

Here is my code: 这是我的代码:

<?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);

?>

And here is the error message: 这是错误消息:

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

It seems that the $client->getAccessToken() isn't set but I don't know why. 似乎未设置$ client-> getAccessToken(),但我不知道为什么。

Thanks in advance for your help. 在此先感谢您的帮助。

Create service account key and download the *.json file that contains private keys. 创建服务帐户密钥并下载包含私钥的* .json文件。 Put the *.json file you just downloaded in a directory of your choosing ( credentials.json file in the example ). 将刚刚下载的* .json文件放在您选择的目录中(本示例中的credentials.json文件)。 Then go to Google Calendar->Share and add service account id on the list. 然后转到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 );

You can find the Google APIs PHP Client Library package with autoloader in the releases page on GitHub, so the only require_once call necessary is for autoload file: 您可以在谷歌API的PHP客户端库包在自动装版本在GitHub上页,所以只能require_once需要可来电是自动加载的文件:

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

And, Google docs may help you further, this is just one way that works for me so it may be useful to you as well. 而且, Google文档可能会进一步为您提供帮助,这只是对我有用的一种方法,因此对您也可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM