1

I've been struggling with this problem for quite some time now. Basically, I have a website where the admin can create events, and at the same time, the events should be added to my company's Google Calendar. I have an HTML form, which posts information to a PHP file, which then attempts to add the event to my own Google Calendar. I've currently made a service account and this is what my code looks like:

require_once 'google-api/vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName("House Events");
$client->setAuthConfig("MY AUTH CONFIG FILE");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');

$service = new Google_Service_Calendar($client);

$event = new Google_Service_Calendar_Event(array(
  'summary' => $title,
  'description' => $html,
  'start' => array(
    'dateTime' => $start
  ),
  'end' => array(
    'dateTime' => $end
  )
));

$calendarId = 'MY CALENDAR ID';
$event = $service->events->insert($calendarId, $event);

$calendarListEntry = new Google_Service_Calendar_CalendarListEntry();
$calendarListEntry->setId($calendarId);

$createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);

printf('Event created: %s\n', $event->htmlLink);

This code returns the error "You need to have writer access to this calendar.", and in the actual Google Calendar sharing screen, I cannot give the service account further privileges.

I've also tried the Google Calendar API Quickstart, but I don't want it to be run off the command line. Also, I don't need access to edit the individual user's calendar, I just want to have my code add an event to my calendar. What's the most efficient way to do this?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Advay Ratan
  • 229
  • 1
  • 13

3 Answers3

1

"You need to have writer access to this calendar."

Means you only have read access to the calendar you are trying to write to.

You need to delegate permissions to the service account. service accounts are like dummy users which you can preauthorize access.

Go to the google calendar website and share the calendar with the service account give it write access.

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

I figured the problem out.

The GSuite was blocking me from giving editor access to calendars outside the Suite (which my service account was). I had to contact the administrator and he gave me access, after which I was able to change the permission settings. Thanks for the help.

Advay Ratan
  • 229
  • 1
  • 13
0

@advay-ratan

Your code will work when it looks like this. you can try this

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(Google_Service_Calendar::CALENDAR); //CALENDAR_READONLY - read only scope

$guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false)));
$client->setHttpClient($guzzleClient);

$client->setRedirectUri("YOUR URI");
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setAccessToken($access_token);
$service = new Google_Service_Calendar($client);

$calendarId = 'primary';
$event = new Google_Service_Calendar_Event([
    'summary' => $request->title,
    'description' => $request->description,
    'start' => ['dateTime' => $startDateTime],
    'end' => ['dateTime' => $endDateTime],
       'reminders' => ['useDefault' => true],
    ]);
$results = $service->events->insert($calendarId, $event);
Jatin Mandanka
  • 411
  • 11
  • 16