使用Google日历检索事件时不会显示任何事件

时间:2013-08-23 19:16:00

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

当我尝试在Google日历上检索事件时,我遇到了代码问题。以下是我的代码:

    <?php

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('client id');
$client->setClientSecret('client secret');
$client->setRedirectUri('redirect uri');
$client->setDeveloperKey('developer key');

$cal = new Google_CalendarService($client);

$events = $cal->events->listEvents('email address for google calendar');

echo $events;    
?>

当我打印出谷歌日历的事件时,我得到的只是一个空数组。但是我确信我的谷歌日历中有几个事件。

事实上,我事件尝试使用以下代码添加事件:

$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2013-08-23T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-08-23T10:25:00.000-07:00');
$event->setEnd($end);

echo $events;

$createdEvent = $cal->events->insert('email address for google calendar', $event);

然而,当我执行此代码时,我最终收到一条错误消息:

  

致命错误:未捕获的异常'Google_ServiceException',并显示消息'错误调用POST https://www.googleapis.com/calendar/v3/calendars/'电子邮件地址以用于Google日历'/ events?key ='key'.............第66行......................... lib / google-api-php-client / src / io / Google_REST.php

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

因为它是一个数组,你需要使用print_r而不是echo。尝试更换:

echo $events; 

print"<pre>".print_r($events, true)."</pre>";