我有一些像这样的PHP代码:
$service = new Google_Service_Calendar($client);
$calendarId = 'your calendar id';
$optParams = array(
'timeMin' => date('c'),
'maxResults' => 100,
'singleEvents' => TRUE,
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = $results->getItems();
// in order to use it in javascript
echo json_encode($events);
$ events是预期的数组,但不包含每个事件的日期。我在使用服务帐户之前做了一些测试,每个日期都可以通过属性“start”访问,但不能在我现在获得的列表中访问。任何想法,因为没有适当的文件,我应该得到什么作为回应?顺便说一句。在日历设置中更改服务帐户的共享权没有帮助。
答案 0 :(得分:0)
$ events确实是正确的列表,但它没有包含文档中提到的所有属性的原因是必须通过方法调用来检索某些属性。所以我们需要做的是:
// start of first event
$startDate = $events[0]->getStart();
这就是我现在整个脚本的样子。原谅我的php,从未使用过它
<?php
header('Content-type: application/json');
include_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('your service account json secret');
$client->setApplicationName('your application name');
$client->setScopes(['https://www.googleapis.com/auth/calendar.readonly']);
$service = new Google_Service_Calendar($client);
// make actual request
$calendarId = 'your calendar id';
$optParams = array(
'timeMin' => date('c'),
'maxResults' => 100,
'singleEvents' => TRUE,
'orderBy' => 'startTime',
);
// of type Events.php
$events = $service->events;
// list of items of type Event.php
$eventItems = $events->listEvents($calendarId, $optParams)->getItems();
// compose an result object, we're only interested in summary, location and dateTime atm
// don't know if this is considered proper php code, works though
$result = array();
for ($i = 0; $i < count($eventItems); $i++)
{
$result[$i]->{summary} = $eventItems[$i]->getSummary();
$result[$i]->{location} = $eventItems[$i]->getLocation();
$result[$i]->{startDate} = $eventItems[$i]->getStart()->getDateTime();
}
echo json_encode($result);
?>