Я пытаюсь использовать Google Calendar v3 API с клиентской библиотекой php (v2) и учетной записью службы google, чтобы получить список доступных календарей, но все мои попытки приводят к код состояния 400 «invalid_grant», ошибка «плохого запроса».Ошибка invalid_grant при использовании API календаря Google и клиентской библиотеки PHP
Создал учетную запись службы, загрузил json-файл и поделился календарем с учетной записью клиента client_email.
здесь код я использую:
<?php
require_once 'google-api/vendor/autoload.php';
getCalendars();
function getClient()
{
$key_file_location = '../keys/serviceaccount.json';
$client = new Google_Client();
$client->setApplicationName("Calendar test");
$client->setAuthConfig($key_file_location);
$client->setScopes(['https://www.googleapis.com/auth/calendar.readonly','https://www.googleapis.com/auth/calendar']);
return $client;
}
function getCalendars()
{
$client = getClient();
$calsInfo = array();
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
$calObj = new stdClass();
$calObj->id = $calendarListEntry->getId();
$calObj->summary = $calendarListEntry->getSummary();
$calObj->description = $calendarListEntry->getDescription();
array_push($calsInfo, $calObj);
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
echo '################ start calendar list: ##################<br />';
echo '<pre>'; print_r($calsInfo); echo '</pre>';
echo '################ end calendar list: ####################<br />';
}
?>
Каждый раз, когда я бегу сценарий, я получаю следующее сообщение об ошибке:
[:error] [pid 214] [client ::1:51761] PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message '{\n "error": "invalid_grant",\n "error_description": "Bad Request"\n}\n' in /Users/kona/Sites/google/google-api/src/Google/Http/REST.php:134\nStack trace:\n#0 /Users/kona/Sites/google/google-api/src/Google/Http/REST.php(92): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\\Psr7\\Response), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...')\n#1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\\Client), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...')\n#2 /Users/kona/Sites/google/google-api/src/Google/Task/Runner.php(181): call_user_func_array(Array, Array)\n#3 /Users/kona/Sites/google/google-api/src/Google/Http/REST.php(57): Google_Task_Runner->run()\n#4 /Users/kona/Sites/google/google-api/src/Google/Client.php(758): Google_Http_REST::execute(Object(GuzzleHttp\\Client), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...', Array)\n#5 /Users/kona/Sites/google/google-api/src/Google/Service/Resource.php(232): Google_Client->execute(O in /Users/kona/Sites/google/google-api/src/Google/Http/REST.php on line 134
есть кто-нибудь может пролить некоторый свет на ошибку?