Question

The documentation for google calendar api is out of date, I am unable to get even a simple calendar list. Here is my code so 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() as $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 a lot of this code from different sites because the docs were out of date, now I'm totally stuck as I've been googling all day. Any insight?

All i get so far is a response like this with 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
        (
        )

)
Was it helpful?

Solution

Allright, this is embarassing after all the time I spent on this. For some reason I never tried making the calendar public. I just assumed that with all the authorization steps that you could access the calendar from project account. Guess not.

Easy fix... just make the calendar public in its settings.

for events replace $cal->calendarlist part with

$cal->events->listEvents('calendarID', $optionalParams);

Note: I'm not even sure all the authorization/access token stuff in session is needed it seems the docs page of google cal still uses the old php client. I believe it should work using only the service account (setAssertionCredentials) the rest with the session im not sure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top