2016-09-20 81 views
1

我使用Google日曆PHP API從日曆獲取事件。使用相同設置訪問另一個日曆 - Google Calendar API PHP

一切工作與最初的日曆,但現在我試圖使用相同的設置從另一個日曆(相同的谷歌帳戶)接收事件。

有在日誌中以下錯誤:

PHP致命錯誤:未捕獲的異常 'Google_Service_Exception' 與消息 '錯誤調用GET https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID.calendar.google.com/events?maxResults=999&orderBy=startTime&singleEvents=true&timeZone=Europe%2FTallinn&timeMin=2016-09-19T00%3A01%3A00Z&timeMax=2018-07-17T00%3A00%3A00Z:(404)未找到' 是/ www /阿帕奇/域/萬維網。 domain.ee/htdocs/wp-content/themes/THEME/booking/vendor/google/apiclient/src/Google/Http/REST.php:79

而這裏的代碼(只有我chaged就是$calendarId

require_once 'vendor/autoload.php'; 

$client_email = '[email protected]'; 
$private_key = file_get_contents('name.p12'); 
$scopes = array('https://www.googleapis.com/auth/calendar'); 
$credentials = new Google_Auth_AssertionCredentials(
    $client_email, 
    $scopes, 
    $private_key 
); 

$client = new Google_Client(); 
$client->setAssertionCredentials($credentials); 
if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion(); 
} 

$service = new Google_Service_Calendar($client); 

// Print the next 999 events on the user's calendar. 
$calendarId = '[email protected]'; 
$optParams = array(
    'maxResults' => 999, 
    'orderBy' => 'startTime', 
    'singleEvents' => TRUE, 
    'timeZone' => 'Europe/Tallinn', 
    'timeMin' => '2016-09-19T00:01:00Z', 
    'timeMax' => '2018-07-17T00:00:00Z', 
); 
$results = $service->events->listEvents($calendarId, $optParams); 

將相同的數據插入到Google API Explorer中會返回我正在查找的事件。

回答

1

從Google Calendar API中的處理API錯誤中,您得到某個時間的error 404意味着未找到指定的資源。其他可能的原因是請求的資源(使用提供的ID)從未存在或訪問用戶無法訪問的日曆時。

這裏建議採取的措施是使用exponential backoff

從這個SO question基礎,另一個導致出現此問題是一個service account訪問私人日曆時,你需要的,如果你擁有包含這些日曆的域或將需要共享的私人日曆要麼執行權限授權的服務帳戶的電子郵件地址。

+1

就是這樣,我忘記與服務的電子郵件共享日曆!非常感謝! –