如何使用Google Calendar API v3/Google API客户端库显示所有可用日历的列表?
我一直在尝试使用PHP访问Google Calendar API v3.最初,我只想简单列出我的API调用可访问的用户日历.
I have been trying to access the Google Calendar API v3 using PHP. Initially, I want to simply list the user calendars that are accessible to my call to the API.
为此,我已经下载了Google API PHP客户端库,并尝试使用以下代码(经过改编而得自
To do so, I have downloaded the Google API PHP Client Library and have attempted to use the following code (which is sourced, with my adaptations, from 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 = '754674507864-pm1dsgdsgsdfsdfsdfdflg6lt9a@developer.gserviceaccount.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详细信息,我用来设置相应的变量,如从代码中可以看到的那样.
I have created a service account in the Google Developers console, and generated OAuth details, which I have used to set the appropriate variables as can be seen from the code.
此代码返回以下内容:
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] => bots@madeupcompany.co.uk [summary] => bots@madeupcompany.co.uk [timeZone] => Europe/London [colorId] => 23 [backgroundColor] => #cd74e6 [foregroundColor] => #000000 [selected] => 1 [accessRole] => reader [defaultReminders] => Array ( ) ) ) ) [processed:protected] => Array ( ) )
问题在于,这似乎仅返回一个日历的详细信息.也就是bots@madeupcompany.co.uk(我与服务明确共享的唯一日历)的日历.
The problem is that this seems to be returning details of only one calendar. That is, the calendar for bots@madeupcompany.co.uk (the only one I explicitly shared with my service).
但是,我知道这个Google帐户对许多其他用户的日历具有只读访问权限(以该用户身份登录Google日历时,我可以看到它们).
However, I know this google account has read-only access to a number of other user's calendars (I can see them when logged into Google Calendar as this user).
此外,如果我在此页面上使用Google Apps Explorer:
Furthermore, if I use the Google Apps Explorer on this page: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list#auth , while logged into my google account as bots@madeupcompany.co.uk, I get details of all of these other calendars.
所以我正在尝试解决问题,为什么Apps Explorer会向我显示所有其他日历,而PHP代码却没有?
So I'm trying to work out, why is the Apps Explorer showing me all the other calendars, and yet the PHP code is not?
服务帐户不需要提示用户访问权限,因为您必须对其进行设置.转到Google日历网站.找到日历设置",然后转到日历"选项卡,找到要访问的日历,然后单击共享:编辑设置",就像添加个人电子邮件地址一样添加服务帐户电子邮件地址.就像您与任何其他用户共享服务帐户一样,这将赋予该服务帐户相同的访问权限.
A service account doesn’t need to prompt a user for access because you have to set it up. Go to the Google Calendar website. Find the Calendar Settings , then go to the Calendars tab, find the calendar you want to access and click on "Shared: Edit settings" add the service account email address like you would a persons email address. This will give the service account the same access as if you where sharing it with any other user.
<?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 = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.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>