Google Calendar API showing error of insufficient permission.
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/calendar/v3/users/me/calendarList: (403) Insufficient Permission' in
<?php
session_start();
include_once "templates/base.php";
require_once realpath(dirname(__FILE__)'/../src/Google/autoload.php');
$client_id = '110224493213908548123'; //Client ID .apps.googleusercontent.com
$service_account_name = '[email protected]'; //Email Address
$key_file_location = 'test.p12'; //key.p12
$client = new Google_Client();
$client->setApplicationName("Easycarcare");
$client->addScope("https://www.googleapis.com/auth/calendar");
//$client->addScope("https://www.googleapis.com/auth/calendar.readonly");
$service = new Google_Service_Calendar($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/calendar'),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$calendarList = $service->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary();
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}