Using a service account, I am trying to insert an event in a target user's calendar. The execution seems to succeed but no event is posted. This is somewhat similar to the issue at Google Calendar API v3 - Not Creating Event (Server-to-Server Authentication) except I'm using the apiclient installed by the composer directive composer require google/apiclient:^2.0
. Thus the use of $client->setAuthConfig
instead of Google_Auth_AssertionsCredentials()
.
I've verified that the target user's calendar is set to make changes to events by the service account email (svcacctabc@xyz.iam.gserviceaccount.com), and that the proper service-account-credentials.json file is present. I've also researched many related entries at Stack Overflow and elsewhere. The test code is below, any ideas as to what I may be missing?
<?php
require_once __DIR__ . '/vendor/autoload.php';
$KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';
$scopes = array('https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly');
$client = new Google_Client();
$client->setApplicationName("Calendar API Test");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes($scopes);
$client->setAccessType('offline');
$service = new Google_Service_Calendar($client);
$calendarId = 'examplecalendaruser@gmail.com';
$event = new Google_Service_Calendar_Event(array(
'summary' => 'Test inserted event',
'description' => 'Test inserted description',
'start' => array(
'dateTime' => '2016-09-02T11:00:00-04:00',
),
'end' => array(
'dateTime' => '2016-09-02T12:00:00-04:00',
),
));
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
echo "\nEvent created: " . $event->htmlLink;
?>
Note that an event link is created, but browsing it results in a "This event does not exist" message (and no event is displayed on the user calendar).