0

I am trying to create secondary calendars for a calendar that is shared with a service account. My code is as follows:

putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service_account.json');
define('SCOPES', Google_Service_Calendar::CALENDAR);

    $client = new Google_Client();
    $client->setApplicationName('REdu Calendars');
    $client->useApplicationDefaultCredentials();
    $client->addScope([SCOPES]);
    $client->setAccessType('offline');

    $service = new Google_Service_Calendar($client);

    $calendar = new Google_Service_Calendar_Calendar();
    $calendar->setSummary($userName);
    $calendar->setTimeZone('America/Los_Angeles');
    $createdCalendar = $service->calendars->insert($calendar);

I think the above code creates a secondary calendar in the service account. What I really want is the ability to create a secondary calendar in the calendar that is shared with the service account. Any help is greatly appreciated.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
itsMeMoriarty
  • 125
  • 1
  • 1
  • 9

1 Answers1

0

To create a secondary calendar for a user who shared his calendar with the service account, you need to impersonate the owner of the calendar

Requirements:

  • You need to know the email of the calendar owner
  • The user must be a suer of your domain
  • Domain-wide delegation must be anabled for the given service account
  • The necessary scopes must be granted in the Admin console
  • The request to impersonate a user must be incorporated into your code when building the service account

Sample for PHP:

    $client = new Google_Client();
    $client->setSubject('ownerOfCalendar@domain.com');
    $client->setApplicationName('REdu Calendars');
    $client->useApplicationDefaultCredentials();
    $client->addScope([SCOPES]);
    $client->setAccessType('offline');

    $service = new Google_Service_Calendar($client);
ziganotschka
  • 25,866
  • 2
  • 16
  • 33