Доступ к событиям календаря Google из учетной записи службы: {"error": "access_denied"}. Нет приложений Google

Я хотел бы получить доступ к календарю Google, используя учетную запись службы. Это мой код: <NUMBER> заменяет правильное значение на Google API Console.

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

Когда я выполняю свой код, я получаю:

Неустранимая ошибка: исключить исключение «Google_AuthException» с сообщением «Ошибка обновления токена OAuth2, сообщение:« {«error»: «access_denied»} '' в /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php : 279 Трассировка стека: # 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-> знак (объект (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) Доступ не настроен

Что здесь происходит?

Related of "Доступ к событиям календаря Google из учетной записи службы: {"error": "access_denied"}. Нет приложений Google"

У меня есть решение. Сначала хороший код является последним:

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

Проблема заключается не в коде, а в моей учетной записи google, я упомянул Referer в Google API Console. После стирания поля код работает.

Для информационных целей, если вы хотите получать доступ к общим календарям, не забудьте поделиться своим календарем с вашим XXXXXX@developer.gserviceaccount.com (ваш SERVICE_ACCOUNT_NAME).

Для получения дополнительной информации см.

https://groups.google.com/forum/?fromgroups=#!topic/google-ajax-search-api/kaKYuUstwB0/discussion