Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
google api php客户端与key.p12连接_Php_Oauth 2.0_Google Api Php Client - Fatal编程技术网

google api php客户端与key.p12连接

google api php客户端与key.p12连接,php,oauth-2.0,google-api-php-client,Php,Oauth 2.0,Google Api Php Client,我可以使用API密钥和OAuth 2.0客户端ID将事件添加到我的google日历中,但我想在没有google授权屏幕的情况下这样做 我遵循了这篇帖子上的信息: stackoverflow.com/questions/8995451/how-do-i-connect-to-the-google-calendar-api-without-the-oauth-authentication 为此,我创建了一个“服务帐户密钥”。我转到我的项目凭据并选择“创建凭据>服务帐户密钥” 然后: 服务帐户>我的项

我可以使用API密钥和OAuth 2.0客户端ID将事件添加到我的google日历中,但我想在没有google授权屏幕的情况下这样做

我遵循了这篇帖子上的信息:

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

为此,我创建了一个“服务帐户密钥”。我转到我的项目凭据并选择“创建凭据>服务帐户密钥”

然后:

服务帐户>我的项目名称

键类型>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);

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


提前感谢您的帮助。

创建服务帐户密钥并下载包含私钥的*.json文件。将刚下载的*.json文件放在您选择的目录中(示例中为
credentials.json
文件)。然后转到Google Calendar->Share并在列表中添加服务帐户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的页面上找到带有autoloader的Google API PHP客户端库包,因此唯一需要的
require\u once
调用是自动加载文件:

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

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

谢谢Danijel的回答。我使用角色>项目>所有者创建了一个新的服务帐户凭据。我下载了该文件并将其放在我的webserer的根目录中。我被“然后转到谷歌日历->共享并在列表中添加服务帐户id”所困扰。我转到我的谷歌帐户日历>设置>日历>我的日历>日历地址>共享设置>与特定人员共享,在这里我尝试输入“服务帐户密钥id”,但没有成功。它请求一个电子邮件地址。我遗漏了什么吗?是的,我写错了,我使用了“管理服务帐户”,并使用了“服务帐户ID”,而不是“密钥ID”,这是我的错误。
require_once '/path/to/google-api-php-client/vendor/autoload.php';