0

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).

Community
  • 1
  • 1
lisaais
  • 1
  • 1
  • 2
  • Are you (the computer running the program) the user or is it someone else? In other words, is it your calendar you want to add the event to or is it someone else's calendar? – Matt Cremeens Aug 30 '16 at 19:41
  • The reason I ask is because I believe you need to replace `'primary'` with the email address associated with the calendar in question. For some reason you have it that way before `$event` but then you change it to `primary` just prior to the insertion. – Matt Cremeens Aug 30 '16 at 19:43
  • Ah, *thank you* Matt, I missed that 'leftover' line from earlier trials...removing it did the trick! I should also note that since my original post, I experimented with executing the AclRuleScope/AclRule code shown in the answer to http://stackoverflow.com/questions/22720387/google-calendar-api-v3-not-creating-event-server-to-server-authentication , which finally seemed to get something on the target calendar (albeit only from the service account share). Thanks again, your 2nd set of eyes helped!! – lisaais Aug 31 '16 at 13:24
  • Glad I could help. I wouldn't have been able to help even a few weeks ago. It just so happened that I've been struggling through this api recently myself. I share your woes! – Matt Cremeens Aug 31 '16 at 13:30

0 Answers0