我一直在嘗試使用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代碼不是?
您是否嘗試過通過日曆服務添加日曆本身並檢查那些被上市? https://developers.google.com/google-apps/calendar/v3/reference/calendars/insert – 2014-12-05 19:21:22