2014-11-20 67 views
0

我無法使用Google提供的PHP庫閱讀日曆活動。我想閱讀的日曆不公開共享,但我想在我的服務器應用程序上閱讀它們。使用PHP API的Google日曆服務帳戶oauth2.0 - 如何訪問未共享的日曆

持有日曆的帳戶與我的帳戶分開(我將其稱爲API帳戶),但日曆與我共享。

根據對谷歌並未公開共享日曆細節:

Anyone can: See nothing

You can: See only free/busy (hide details).

這兩個API帳戶,我的帳戶有一個P12鍵OAuth2.0用戶服務帳戶。

我按照指南:http://www.daimto.com/google_service_account_php/這幫助我弄清楚了身份驗證過程。我已設法閱讀在任一帳戶中公開分享的日曆,而不會有任何問題。這裏是我的代碼:

// Start the Google client 
$client = new Google_Client(); 
$client->setClientId({{client_id from google}}); 
$client->setApplicationName("Project Name"); 
$client->setClientSecret({{client_secret from google}}); 
$key = file_get_contents({{key_file_location on my server}}); 
$cred = new Google_Auth_AssertionCredentials(
     $Email_address, 
     // Scopes that we will be using 
     array(
      "https://www.googleapis.com/auth/calendar" 
     ), 
     $key 
); 

$client->setAssertionCredentials($cred); 

if($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} 

// Use Google Calendar API plugin 
$service = new Google_Service_Calendar($client); 

$optParams = array(
    'singleEvents'=> true, 
    'timeMin'=>date(DateTime::ATOM), 
    'timeMax' => date(DateTime::ATOM, time()+(3 * 24 * 60 * 60)) 
); 

$results = $service->events->listEvents(
    '{{Calendar ID from Google Calendar Settings}}', 
    $optParams 
); 

然而,嘗試使用代碼未公開共享的日曆時,我得到任何帳戶以下錯誤:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/calendar/v3/calendars {{CalendarID}}:(404) Not Found...

當我嘗試var_dump($client->getAuth()),我注意到,有一個值可能表明身份驗證不起作用:["authenticated":"Google_Client":private]=> bool(false)。這種情況發生在兩個帳戶上,包括公開共享和公共未共享的日曆。

我可以通過我的帳戶使用Google API Explorer在日曆上顯示未公開共享的JSON數據。 Google PHP API或Google帳戶設置中是否存在缺失的內容,可以讓我按照自己的意願進行操作?

謝謝你的時間。

回答

2

如果要使用服務帳戶訪問私人日曆,如果您擁有包含這些日曆的域(https://developers.google.com/drive/web/delegation描述它用於驅動器,但適用於日曆),則需要執行授權委派,否則您需要將私人日曆與服務帳戶的電子郵件地址分享。

0

感謝您的建議。

事實證明,問題在於API帳戶負責人沒有與開發者帳戶的服務器帳戶電子郵件共享日曆。

由於我無法訪問API帳戶設置,並且來自Google Calendar API的錯誤不是很具描述性,所以花了一段時間才弄清楚。

相關問題