[Php] 使用服務帳戶訪問Google日曆事件:{“error”:“access_denied”}。 沒有谷歌應用程序


Answers

Question

我想使用服務帳戶訪問谷歌日曆。 這是我的代碼: <NUMBER>被在Google API控制台上的正確值取代。

<?php

require_once 'googleapi/Google_Client.php';
require_once 'googleapi/contrib/Google_CalendarService.php';

const CLIENT_ID = '<NUMBER>.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME  = '<NUMBER>@developer.gserviceaccount.com';
const MY_EMAIL  = '<MY NAME>@gmail.com';
const KEY_FILE = 'privatekey.p12';

$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setApplicationName("<APP NAME>");

$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/calendar'),
    $key,
    'notasecret',
    'http://oauth.net/grant_type/jwt/1.0/bearer',
    MY_EMAIL)
);

$cal = new Google_CalendarService($client);
$calList = $cal->calendarList->listCalendarList();

print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

當我執行我的代碼時,我收到:

致命錯誤:在/home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php中,未收到異常'Google_AuthException'消息'錯誤刷新OAuth2令牌,消息:'{“error”:“access_denied”}“ :279 Stack trace:#0 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php(256):Google_OAuth2-> refreshTokenRequest(Array)#1 / home / www / 65683f67e3f0d94b14bba3c945014cda / web / intranet / googleapi /auth/Google_OAuth2.php(209):Google_OAuth2-> refreshTokenWithAssertion()#2 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/service/Google_ServiceResource.php(166):Google_OAuth2-> sign(Object(Google_HttpRequest)) #3 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/contrib/Google_CalendarService.php(154):Google_ServiceResource - > __ call('list',Array)#4 / home / www / 65683f67e3f0d94b14bba3c945014cda / web / intranet / testService。 PHP(32):Google_CalendarListServiceResource-> listCalendarList()#5 {main} throw n在/home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php在線279

如果我更改我的代碼:

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

我收到:

(403)訪問未配置

這裡怎麼了?