如何使用 Google Calendar API v3/Google API 客户端库显示*所有*可用日历的列表?

Wil*_*don 5 php google-calendar-api google-api google-client google-api-php-client

我一直在尝试使用 PHP 访问 Google Calendar API v3。最初,我想简单地列出我可以通过 API 调用访问的用户日历。

为此,我下载了 Google 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 = '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);

?>
Run Code Online (Sandbox Code Playgroud)

我在 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] => 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 ( ) )
Run Code Online (Sandbox Code Playgroud)

问题是这似乎只返回一个日历的详细信息。也就是说,bots@madeupcompany.co.uk 的日历(我与我的服务明确共享的唯一日历)。

但是,我知道这个谷歌帐户对许多其他用户的日历具有只读访问权限(当我以该用户身份登录谷歌日历时可以看到它们)。

此外,如果我在此页面上使用 Google Apps Explorer:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list#auth,同时以 bots@madeupcompany 身份登录我的 Google 帐户。 co.uk,我获得了所有其他日历的详细信息。

所以我想弄清楚,为什么 Apps Explorer 向我显示了所有其他日历,而 PHP 代码却没有?

DaI*_*mTo 1

服务帐户\xe2\x80\x99 不需要提示用户进行访问,因为您必须对其进行设置。访问 Google 日历网站。找到“日历设置”,然后转到“日历”选项卡,找到您要访问的日历,然后单击 \xe2\x80\x9cShared:编辑设置\xe2\x80\x9d 添加服务帐户电子邮件地址,就像添加个人电子邮件地址一样。这将为服务帐户提供相同的访问权限,就像您与任何其他用户共享它一样。

\n\n
<?php\nsession_start();        \nrequire_once \'Google/Client.php\';\nrequire_once \'Google/Service/Calendar.php\';     \n/************************************************   \n The following 3 values an befound in the setting   \n for the application you created on Google      \n Developers console.         Developers console.\n The Key file should be placed in a location     \n that is not accessable from the web. outside of \n web root.   \n\n In order to access your GA account you must    \n Add the Email address as a user at the     \n ACCOUNT Level in the GA admin.         \n ************************************************/\n$client_id = \'1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com\';\n$Email_address = \'1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com\';     \n$key_file_location = \'629751513db09cd21a941399389f33e5abd633c9-privatekey.p12\';     \n$client = new Google_Client();      \n$client->setApplicationName("Client_Library_Examples");\n$key = file_get_contents($key_file_location);    \n// seproate additional scopes with a comma   \n$scopes ="https://www.googleapis.com/auth/calendar.readonly";   \n$cred = new Google_Auth_AssertionCredentials(    \n    $Email_address,      \n    array($scopes),     \n    $key         \n    );      \n$client->setAssertionCredentials($cred);\nif($client->getAuth()->isAccessTokenExpired()) {        \n    $client->getAuth()->refreshTokenWithAssertion($cred);       \n}       \n$service = new Google_Service_Calendar($client);    \n\n?>\n\n<html><body>\n\n<?php\n$calendarList  = $service->calendarList->listCalendarList();\nprint_r($calendarList);\nwhile(true) {\n    foreach ($calendarList->getItems() as $calendarListEntry) {\n        echo "<a href=\'Oauth2.php?type=event&id=".$calendarListEntry->id." \'>".$calendarListEntry->getSummary()."</a><br>\\n";\n    }\n    $pageToken = $calendarList->getNextPageToken();\n    if ($pageToken) {\n        $optParams = array(\'pageToken\' => $pageToken);\n        $calendarList = $service->calendarList->listCalendarList($optParams);\n    } else {\n        break;\n    }\n}    \n\n?>\n</html> \n
Run Code Online (Sandbox Code Playgroud)\n\n

使用 PHP \xe2\x80\x93 服务帐户从教程 Google Calendar API 中提取的代码

\n