0
votes

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;
    }
  }
2
What authentication sample are you following? You haven't applied the service account anywhere that I can see. github.com/google/google-api-php-client/blob/master/examples/… tip grab the Json service account key file not the p12 file - DaImTo
@DaImTo I have used p12 key for book api and it was working . So Why this isn't working for calendar - Sintu Roy
Because by default a service account doesn't have any calendars you need to add one first. or give it access to one of yours. Either way calendarlist is pretty useless IMO for service accounts as its only a visual thing and there is no visual web view of a service accounts google calendar account. - DaImTo
also you might want to figure out exactly which line is throwing the error none of the code you have given looks like something that would throw the error you are getting. - DaImTo
@DaImTo I think now its working with with p12 key because when I am doing var_dump its generating new - nextSyncToken everytime but modelData items showing empty When I am using google api testing tools its showing items of my calendar developers.google.com/google-apps/calendar/v3/reference/… Why I am getting calendar items empty? - Sintu Roy

2 Answers

0
votes

You need to remember that a service account is not you. A service account is a dummy user. It has its own google calendar account, its own google drive account and probably a bunch more.

You are not going to see any data on calendar list until you first create a calendar and then add it to the calendar list.

Remember calendar list is not a list of the calendars a user has access to it is just the display in the web view on the bottom left hand corner. So even if you have shared one of your personal calendars with your service account its not going to appear in the services accounts calendar list until you programmatically add it to that list.

Answer: you are not seeing any data because the service account has no data.

0
votes

Have you tried adding your email id '[email protected]' in your google account.

This might be because of the fact that this service account doesn't have enough permission for calender API.