JMGECH
JMGECH

Reputation: 653

Service Account connection to Google Calendar

I am attempting to connect to a Google Calendar, and have setup up a Service Account with domain wide delegation. I then downloaded the json p12 file, placed it on my web server and attempted to run the following code.

The goal is to have a php script return a list of events from a Google Calendar so they can be worked with.

When the code is executed, $events doesn't contain any of the items listed on the calendar, but a json object similar de the one below the php source.

Have I misread the documentation, am I missing something obvious?

Thank you kindly for the feedback,

Nate

<? require_once 'vendor/autoload.php';

    $calendar = initCalendar();

    $events = $calendar->events->listEvents('primary');

    // Take from : https://developers.google.com/calendar/v3/reference/events/list
    while(true) {
      foreach ($events->getItems() as $event) {
        echo $event->getSummary();
      }
      $pageToken = $events->getNextPageToken();
      if ($pageToken) {
        $optParams = array('pageToken' => $pageToken);
        $events = $service->events->listEvents('primary', $optParams);
      } else {
        break;
      }
    }

    function initCalendar(){

        $KEY_FILE_LOCATION = __DIR__ . '/my_downloaded_file.json';

        $client = new Google_Client();

        putenv('GOOGLE_APPLICATION_CREDENTIALS='.$KEY_FILE_LOCATION);
        $client->setAuthConfig($KEY_FILE_LOCATION);
        $client->useApplicationDefaultCredentials();


        $client->setApplicationName('Calendar Sync');
        $client->setIncludeGrantedScopes(true);   // incremental auth       
        $client->setScopes([
            Google_Service_Calendar::CALENDAR
            , Google_Service_Calendar::CALENDAR_READONLY
        ]);     


        $client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/api/events.php');
        $client->setAccessType('offline');        // offline access


        $calendar = new Google_Service_Calendar($client);       

        return $calendar;   

    }


?>

Returned JSON

object(Google_Service_Calendar_Events)#58 (19) {
  ["collection_key":protected]=>
  string(5) "items"
  ["accessRole"]=>
  string(5) "owner"
  ["defaultRemindersType":protected]=>
  string(37) "Google_Service_Calendar_EventReminder"
  ["defaultRemindersDataType":protected]=>
  string(5) "array"
  ["description"]=>
  NULL
  ["etag"]=>
  string(18) ""sdfsdfgsdfsf""
  ["itemsType":protected]=>
  string(29) "Google_Service_Calendar_Event"
  ["itemsDataType":protected]=>
  string(5) "array"
  ["kind"]=>
  string(15) "calendar#events"
  ["nextPageToken"]=>
  NULL
  ["nextSyncToken"]=>
  string(28) "sdfsdfsdfsdfsdf="
  ["summary"]=>
  string(67) "[email protected]"
  ["timeZone"]=>
  string(3) "UTC"
  ["updated"]=>
  string(24) "2018-09-20T10:17:37.161Z"
  ["internal_gapi_mappings":protected]=>
  array(0) {
  }
  ["modelData":protected]=>
  array(0) {
  }
  ["processed":protected]=>
  array(0) {
  }
  ["defaultReminders"]=>
  array(0) {
  }
  ["items"]=>
  array(0) {
  }
}

Upvotes: 1

Views: 61

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116958

A service account is s dummy user it is not your personal user. it has its own Google calendar account. When you supply primary for the calendar if it is the primary calendar for the current authenticated user which is the service account.

You can add new events to the service accounts calendar or share your calendar with the service account then you will need to supply the calendar id of your calendar inorder to access that data.

Upvotes: 1

Related Questions