使用 API v3 访问 Google 日历


Accessing Google Calendar using API v3

我正在尝试重写一个我以前没有使用Google Calendar v3 API编写的旧应用程序。

我已经实现了google-api-php-client,在我的开发人员控制台中创建了一个API密钥,并完成了身份验证的过程。我有一个accessToken,我正在数据库中保留,当它过期时,我可以很好地刷新该令牌。

但是,我根本无法使用日历。

我正在遵循已经存在的有限文档,并具有以下内容:

$this->client = new Google_Client();
$this->client->setClientId( $this->settings['clientId'] );
$this->client->setClientSecret( $this->settings['clientSecret'] );
$this->client->setAccessToken( $this->settings['accessToken'] );
$service = new Google_Service_Calendar($this->client);
$calendarList = $service->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
    echo $calendarListEntry->getSummary();
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
    $optParams = array('pageToken' => $pageToken);
    $calendarList = $service->calendarList->listCalendarList($optParams);
} else {
    break;
    }
}

我得到的错误是:

Message:  Undefined property: Google_Service_Calendar_CalendarList::$items

我已经检查了我的Google日历帐户,并且所有日历都是共享的,因此应该没有问题,尽管我不明白为什么这是一个问题,因为我使用的是经过身份验证的帐户。

有人能够提供建议吗?

非常感谢

您遇到的错误

Message:  Undefined property: Google_Service_Calendar_CalendarList::$items

是因为对象为空

取代

while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
    echo $calendarListEntry->getSummary();
}Undefined property

有了这个来摆脱错误。

if ($calendarList->getItems()) {
    foreach ($calendarList->getItems() as $calendarListEntry) {
    echo $calendarListEntry->getSummary();
    }

同时调整以下内容

$this->client = new Google_Client();

需要

$client = new Google_Client();