I'm trying to read my Google Calendar events using PHP and Googles official PHP client library.
I created a service account in the Google Developers Console and it seemed to work, I can connect to the calendar but I get an empty array:
Google_Service_Calendar_CalendarList Object ( [collection_key:protected] => items [internal_gapi_mappings:protected] => Array ( ) [etag] => [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => array [kind] => [nextPageToken] => [nextSyncToken] => [modelData:protected] => Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain] => global [reason] => insufficientPermissions [message] => Insufficient Permission ) ) [code] => 403 [message] => Insufficient Permission ) ) [processed:protected] => Array ( ) )
my code:
define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
session_start();
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setAuthConfigFile('client_secret.json');
$client->setAccessType('offline');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
#$client->setUseObjects(true);
$cal=new Google_Service_Calendar($client);
$calendarList=$cal->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry;
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Can anybody tell me what I do wrong Thanks in advance for your help.