在时间同步时使用Google服务帐户的invalid_grant错误

时间:2013-01-02 13:30:36

标签: oauth-2.0 ntp

我已经调试了这个问题好几天了,但我想不出其他任何事情来试图解决这个问题。

错误

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{
  "error" : "invalid_grant"
}'' in /html/cms/php/google-api-php-client/src/auth/Google_OAuth2.php:279
Stack trace:
#0 /html/cms/php/google-api-php-client/src/auth/Google_OAuth2.php(256): Google_OAuth2->refreshTokenRequest(Array)
#1 /html/cms/php/google-api-php-client/src/auth/Google_OAuth2.php(209): Google_OAuth2->refreshTokenWithAssertion()
#2 /html/cms/php/google-api-php-client/src/service/Google_ServiceResource.php(166): Google_OAuth2->sign(Object(Google_HttpRequest))
#3 /html/cms/php/google-api-php-client/src/contrib/Google_CalendarService.php(154): Google_ServiceResource->__call('list', Array)
#4 /html/cms/php/google_calendar_sync.php(41): Google_CalendarListServiceResource->listCalendarList()
#5 {main}
  thrown in /html/cms/php/google-api-php-client/src/auth/Google_OAuth2.php on line 279

我的服务器时钟同步:  1月2日13:35:36 ntpdate [1214]:调整时间服务器62.212.76.57偏移0.003544秒 (62.212.76.57是我的colo公司'时间服务器)

我已经在Google API控制台上检查了我的服务帐户开发人员凭据(我有一个屏幕截图,但我无法使用它,因为我的帐户太新了:/)。我也成功订阅了日历服务(也无法使用我制作的截图)。

我的代码

<?php

    require_once(dirname(__FILE__) . '/google-api-php-client/src/Google_Client.php');
    require_once(dirname(__FILE__) . '/google-api-php-client/src/contrib/Google_CalendarService.php');

    session_start();

    define('CLIENT_ID', 'XXXXXX510496.apps.googleusercontent.com');
    define('SERVICE_ACCOUNT_NAME', 'XXXXXX510496@developer.gserviceaccount.com');
    define('CALENDAR_ID', 'XXXXXXXXXXXXXXXXX');
    define('KEY_FILE', dirname(__FILE__) . '/private/XXXXXXXXXXXXXXXXXXXX881a845ed4e4d2d58eb1-privatekey.p12');

    $client = new Google_Client();

    if (isset($_SESSION['token'])) {
     $client->setAccessToken($_SESSION['token']);
    }

    $key = file_get_contents(KEY_FILE);
    $client->setClientId(CLIENT_ID);
    $client->setAssertionCredentials(new Google_AssertionCredentials(
      SERVICE_ACCOUNT_NAME,
      'https://www.google.com/calendar/feeds/' . CALENDAR_ID . '/private/full/',
      $key)
    );

    $service = new Google_CalendarService($client);

    $calendarList = $service->calendarList->listCalendarList();
    echo "<h1>Calendar List</h1><pre>" . print_r($calendarList, true) . "</pre>";

 ?>

我完全没有想法,所以任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:0)

我刚刚发布了一份文件,可能会帮助您解决问题:

http://www.tqis.com/eloquency/googlecalendar.htm