Google API PHP客户端 - 检索日历列表和事件
-
20-12-2019 - |
题
Google Calendar API的文档已过期,我无法获得一个简单的日历列表。到目前为止,这是我的代码。
<?php
require_once "includes/partials/_header.php";
set_include_path(get_include_path() . '/google-api-php-client/src');
$client_id = 'xxxx.apps.googleusercontent.com';
$service_account = 'xxxx@developer.gserviceaccount.com';
$p12 = 'xxxx-privatekey.p12';
session_start();
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
$client = new Google_Client();
$client->setApplicationName("Calendrier");
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents($p12);
$client->setClientId($client_id);
$cred = new Google_Auth_AssertionCredentials(
$service_account,
array('https://www.googleapis.com/auth/calendar'),
$key);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$cal = new Google_Service_Calendar($client);
$events = $cal->calendarList->listCalendarList();
echo "<pre>";
print_r($events);
echo"</pre>";
while(true) {
foreach ($events->getItems() as $event) {
echo $event->getSummary();
print_r($event);
}
$pageToken = $events->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$events = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
?>
.
我有很多来自不同网站的代码,因为文档已经过时了,现在我完全被困在整天谷歌曲。任何洞察力?
到目前为止,我到目前为止是这样的回复,没有“项目”
Google_Service_Calendar_CalendarList Object
(
[etag] => "xxxxxxxxxxxx"
[itemsType:protected] => Google_Service_Calendar_CalendarListEntry
[itemsDataType:protected] => array
[kind] => calendar#calendarList
[nextPageToken] =>
[nextSyncToken] => 00001401741798955000
[collection_key:protected] => items
[modelData:protected] => Array
(
[items] => Array
(
)
)
[processed:protected] => Array
(
)
)
. 解决方案
偶然,这在我花在这一切的时间之后是尴尬的。出于某种原因,我从未尝试过日历公众。我刚刚假设您可以从项目帐户访问日历的所有授权步骤。猜猜不。
简单修复......只需在其设置中公开日历。
对于事件用
替换$ cal-> calendarlist零件$cal->events->listEvents('calendarID', $optionalParams);
.
注意:我甚至不确定所需的所有授权/访问令牌,似乎Google Cal的文档页面仍然使用旧的PHP客户端。我相信它应该只使用服务帐户(setassertioncredentials)的工作时间,其余的会话我不确定。
不隶属于 StackOverflow