2

我一直在嘗試使用PHP訪問Google Calendar API v3。最初,我想簡單地列出可以訪問API的用戶日曆。如何使用Google Calendar API v3/Google API客戶端庫顯示* all *可用日曆的列表?

要做到這一點,我已經下載了谷歌API PHP客戶端庫,並已嘗試使用下面的代碼(這是採購的,用我的改編,從https://mytechscraps.wordpress.com/2014/05/15/accessing-google-calendar-using-the-php-api/):

<?php 

//error_reporting(0); 
//@ini_set('display_errors', 0); 

// If you've used composer to include the library, remove the following line 
// and make sure to follow the standard composer autoloading. 
// https://getcomposer.org/doc/01-basic-usage.md#autoloading 
require_once './google-api-php-client-master/autoload.php'; 

// Service Account info 
$client_id = '754612121864-pmdfssdfakqiqblg6lt9a.apps.googleusercontent.com'; 
$service_account_name = '[email protected]nt.com'; 
$key_file_location = 'Calendar-7dsfsdgsdfsda953d68dgdsgdsff88a.p12'; 


$client = new Google_Client(); 
$client->setApplicationName("Calendar"); 

$service = new Google_Service_Calendar($client); 

$key = file_get_contents($key_file_location); 
$cred = new Google_Auth_AssertionCredentials(
$service_account_name, 
array('https://www.googleapis.com/auth/calendar.readonly'), 
$key 
); 

$client->setAssertionCredentials($cred); 

$cals = $service->calendarList->listCalendarList(); 
print_r($cals); 

?> 

我創建了一個服務Google Developers控制檯中的帳戶以及生成的OAuth詳細信息,這些詳細信息用於設置可從代碼中看到的適當變量。

此代碼返回如下:

Google_Service_Calendar_CalendarList Object ([collection_key:protected] => items [internal_gapi_mappings:protected] => Array () [etag] => "14171313334000" [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => array [kind] => calendar#calendarList [nextPageToken] => [nextSyncToken] => 000014121268327000 [modelData:protected] => Array ([items] => Array ([0] => Array ([kind] => calendar#calendarListEntry [etag] => "1417721316542000" [id] => [email protected] [summary] => [email protected] [timeZone] => Europe/London [colorId] => 23 [backgroundColor] => #cd74e6 [foregroundColor] => #000000 [selected] => 1 [accessRole] => reader [defaultReminders] => Array ()))) [processed:protected] => Array ()) 

的問題是,這似乎是隻返回一個日曆的細節。也就是[email protected]的日曆(我唯一明確與我的服務共享的日曆)。

但是,我知道這個Google帳戶對多個其他用戶的日曆擁有隻讀權限(我可以在以該用戶身份登錄Google日曆時看到他們)。

而且,如果我使用谷歌Apps的瀏覽器這個頁面上:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list#auth,同時登錄到我的谷歌帳戶[email protected],我得到的所有這些其他日曆的詳細信息。

所以我試圖解決,爲什麼Apps Explorer向我展示所有其他日曆,但PHP代碼不是?

+0

您是否嘗試過通過日曆服務添加日曆本身並檢查那些被上市? https://developers.google.com/google-apps/calendar/v3/reference/calendars/insert – 2014-12-05 19:21:22

回答

0

服務帳戶不需要提示用戶進行訪問,因爲您必須對其進行設置。轉到Google日曆網站。找到日曆設置,然後轉到日曆選項卡,找到您想要訪問的日曆,然後點擊「共享:編輯設置」添加服務帳戶的電子郵件地址,就像您的個人電子郵件地址一樣。這將爲服務帳戶提供與其他用戶共享服務帳戶相同的訪問權限。

<?php 
session_start();   
require_once 'Google/Client.php'; 
require_once 'Google/Service/Calendar.php';  
/************************************************ 
The following 3 values an befound in the setting 
for the application you created on Google  
Developers console.   Developers console. 
The Key file should be placed in a location  
that is not accessable from the web. outside of 
web root. 

In order to access your GA account you must  
Add the Email address as a user at the  
ACCOUNT Level in the GA admin.   
************************************************/ 
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com'; 
$Email_address = '[email protected]eaccount.com';  
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';  
$client = new Google_Client();  
$client->setApplicationName("Client_Library_Examples"); 
$key = file_get_contents($key_file_location);  
// seproate additional scopes with a comma 
$scopes ="https://www.googleapis.com/auth/calendar.readonly"; 
$cred = new Google_Auth_AssertionCredentials( 
    $Email_address,  
    array($scopes),  
    $key   
    );  
$client->setAssertionCredentials($cred); 
if($client->getAuth()->isAccessTokenExpired()) {   
    $client->getAuth()->refreshTokenWithAssertion($cred);  
}  
$service = new Google_Service_Calendar($client);  

?> 

<html><body> 

<?php 
$calendarList = $service->calendarList->listCalendarList(); 
print_r($calendarList); 
while(true) { 
    foreach ($calendarList->getItems() as $calendarListEntry) { 
     echo "<a href='Oauth2.php?type=event&id=".$calendarListEntry->id." '>".$calendarListEntry->getSummary()."</a><br>\n"; 
    } 
    $pageToken = $calendarList->getNextPageToken(); 
    if ($pageToken) { 
     $optParams = array('pageToken' => $pageToken); 
     $calendarList = $service->calendarList->listCalendarList($optParams); 
    } else { 
     break; 
    } 
}  

?> 
</html> 

代碼教程撕開Google Calendar API with PHP – Service Account

相關問題