I have been trying to access the Google Calendar API v3 using PHP. Initially, I want to simply list the user calendars that are accessible to my call to the API.
To do so, I have downloaded the Google API PHP Client Library and have attempted to use the following code (which is sourced, with my adaptations, from https://mytechscraps.wordpress.com/2014/05/15/accessing-google-calendar-using-the-php-api/ ):
<?php
//error_reporting(0);
//@ini_set('display_errors', 0);
// If you've used composer to include the library, remove the following line
// and make sure to follow the standard composer autoloading.
// https://getcomposer.org/doc/01-basic-usage.md#autoloading
require_once './google-api-php-client-master/autoload.php';
// Service Account info
$client_id = '754612121864-pmdfssdfakqiqblg6lt9a.apps.googleusercontent.com';
$service_account_name = '754674507864-pm1dsgdsgsdfsdfsdfdflg6lt9a@developer.gserviceaccount.com';
$key_file_location = 'Calendar-7dsfsdgsdfsda953d68dgdsgdsff88a.p12';
$client = new Google_Client();
$client->setApplicationName("Calendar");
$service = new Google_Service_Calendar($client);
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/calendar.readonly'),
$key
);
$client->setAssertionCredentials($cred);
$cals = $service->calendarList->listCalendarList();
print_r($cals);
?>
I have created a service account in the Google Developers console, and generated OAuth details, which I have used to set the appropriate variables as can be seen from the code.
This code returns the following:
Google_Service_Calendar_CalendarList Object ( [collection_key:protected] => items [internal_gapi_mappings:protected] => Array ( ) [etag] => "14171313334000" [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => array [kind] => calendar#calendarList [nextPageToken] => [nextSyncToken] => 000014121268327000 [modelData:protected] => Array ( [items] => Array ( [0] => Array ( [kind] => calendar#calendarListEntry [etag] => "1417721316542000" [id] => bots@madeupcompany.co.uk [summary] => bots@madeupcompany.co.uk [timeZone] => Europe/London [colorId] => 23 [backgroundColor] => #cd74e6 [foregroundColor] => #000000 [selected] => 1 [accessRole] => reader [defaultReminders] => Array ( ) ) ) ) [processed:protected] => Array ( ) )
The problem is that this seems to be returning details of only one calendar. That is, the calendar for bots@madeupcompany.co.uk (the only one I explicitly shared with my service).
However, I know this google account has read-only access to a number of other user's calendars (I can see them when logged into Google Calendar as this user).
Furthermore, if I use the Google Apps Explorer on this page: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list#auth , while logged into my google account as bots@madeupcompany.co.uk, I get details of all of these other calendars.
So I'm trying to work out, why is the Apps Explorer showing me all the other calendars, and yet the PHP code is not?