【问题标题】:Accessing Google Calendar using API v3使用 API v3 访问 Google 日历
【发布时间】:2015-02-14 02:20:42
【问题描述】:

我正在尝试使用 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 日历帐户并且所有日历都是共享的,所以应该没有问题,尽管我不明白为什么这是个问题,因为我使用的是经过身份验证的帐户。

谁能提供建议?

非常感谢

【问题讨论】:

  • 您是否尝试过在此处调试您的日历列表调用? developers.google.com/google-apps/calendar/v3/reference/… 只是为了确保您收到“$calendarList->getItems()”的回复
  • 是的,当我使用“试试看”功能时,它会很好地返回日历。
  • 你能发布 var_dump($calendarList->getItems()) 的输出吗?
  • 是的,一个返回 NULL 的 var_dump

标签: php api google-calendar-api google-api-php-client


【解决方案1】:

你得到的错误

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();

【讨论】:

  • 那也行不通。返回错误:“Can't use method return value in write context”
  • if($calendarList->getItems()){
  • 如果你用我上面的评论替换第一行是否可以解决问题
  • 它解决了这个特定问题 - 但我仍然收到“消息:未定义属性:Google_Service_Calendar_CalendarList::$items”
  • 更改 $this->client = new Google_Client();到 $client = new Google_Client();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 2015-03-01
相关资源
最近更新 更多