我一直在尝试使用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