Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google calendar api 谷歌API PHP客户端-检索日历列表和事件_Google Calendar Api_Google Api Php Client - Fatal编程技术网

Google calendar api 谷歌API PHP客户端-检索日历列表和事件

Google calendar api 谷歌API PHP客户端-检索日历列表和事件,google-calendar-api,google-api-php-client,Google Calendar Api,Google Api Php Client,谷歌日历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@develope

谷歌日历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;
  }
}
?>

好吧,在我花了这么多时间之后,这真让人尴尬。出于某种原因,我从未尝试公开日历。我只是假设,通过所有授权步骤,您可以从项目帐户访问日历。我想不是

容易解决。。。只需在其设置中公开日历即可

对于事件,将$cal->calendarlist部分替换为


注意:我甚至不确定会话中所有的授权/访问令牌都是必需的,因为google cal的文档页面似乎仍然使用旧的php客户端。我相信它应该只使用服务帐户(setAssertionCredentials)就可以工作,其余的会话我不确定。

我如何按日期列出事件?例如,我只想要从今天开始的事件。可选参数中有order by、start time、end time。我在@user3542112有相同的问题。您将
$optionalParams
设置为什么?我记不清了,但您必须在字符串中包含时区“timeMin和/或timeMax”=>“todays date”,包括时区“尝试检查api返回时得到的结果,并使用相同的日期时间格式timeMin datetime下限(包括在内)用于筛选事件的结束时间。可选。默认情况下,不按结束时间进行过滤。必须是带有强制时区偏移的RFC3339时间戳,例如2011-06-03T10:00:00-07:00、2011-06-03T10:00:00Z
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->events->listEvents('calendarID', $optionalParams);