2016-02-22 39 views
3

我想使用谷歌日曆v3 API與php(v2)客戶端庫和谷歌服務帳戶來獲取可用日曆的列表,但我所有的嘗試導致一個狀態碼400「invalid_grant」,「錯誤請求」錯誤。使用谷歌日曆API和PHP客戶端庫時發生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 

是任何人能擺脫上的錯誤一些輕?

回答

0

看來我的本地服務器存在時間同步問題。更新服務器的時鐘,一切正常。

相關問題