使用Google Calendar API和PHP Client Library时出现invalid_grant错误

时间:2016-02-22 00:23:16

标签: php google-calendar-api

我正在尝试使用带有php(v2)客户端库和Google服务帐户的Google Calendar v3 API来获取可用日历列表,但我的所有尝试都会导致状态码400“invalid_grant”,“错误请求”错误。

我创建了服务帐户,下载了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

是否有人能够对错误有所了解?

1 个答案:

答案 0 :(得分:0)

我的本​​地服务器似乎遇到了时间同步问题。更新了服务器的时钟,一切都很顺利。