Google API PHP Client - retrieve calendar list and events -
the documentation google calendar api out of date, unable simple calendar list. here code far.
<?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() $event) { echo $event->getsummary(); print_r($event); } $pagetoken = $events->getnextpagetoken(); if ($pagetoken) { $optparams = array('pagetoken' => $pagetoken); $events = $service->calendarlist->listcalendarlist($optparams); } else { break; } } ?>
i got lot of code different sites because docs out of date, i'm totally stuck i've been googling day. insight?
all far response no "items"
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 ( ) )
allright, embarassing after time spent on this. reason never tried making calendar public. assumed authorization steps access calendar project account. guess not.
easy fix... make calendar public in settings.
for events replace $cal->calendarlist part with
$cal->events->listevents('calendarid', $optionalparams);
note: i'm not sure authorization/access token stuff in session needed seems docs page of google cal still uses old php client. believe should work using service account (setassertioncredentials) rest session im not sure.
Comments
Post a Comment