Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding an event to google calendar using php

I'm working on a client web app where users can book drives with date, time, location...etc

the client required that every booking is added as an event on his google calendar

I created an API key and downloaded the PHP API client: https://github.com/google/google-api-php-client

But when I try to add an event I get a "Login Required" Error

How can I add an event directly without having to use OAuth and consent screen because the function will be executed automatically in the backend, I have full access to the gmail account.

like image 888
MomenSh Avatar asked Jun 02 '18 10:06

MomenSh


1 Answers

I got it to work using a service account and some steps based on this answer, here is what I did:

1- Create a project on Google Developer Console.

2- Go to Credentials and create a Service account key with key type JSON, a JSON file will be downloaded, move it to your project folder.

3- Enable Calendar API from Library tab, search for Calendar API and enable it.

4- Go to your Google Calendar

5- Go to Settings -> Add calendar -> New calendar, after that a notification/toast will popup click on Configure, scroll down to Share with specific people -> Add people, on the email field add the service account ID, you can get it from Credentials -> Manage service accounts then set the Permission to Make changes to events and click Send.

6- Download the PHP client library.

7- Now you need to get the calendar ID, from calendar settings scroll down to last section you will find it, or here is a sample code to get it, look for it in the response, it will be something like this '[email protected]':

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

$client = new Google_Client();
//The json file you got after creating the service account
putenv('GOOGLE_APPLICATION_CREDENTIALS=google-api/test-calendar-serivce-1ta558q3xvg0.json');
$client->useApplicationDefaultCredentials();
$client->setApplicationName("test_calendar");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');

$service = new Google_Service_Calendar($client);

$calendarList = $service->calendarList->listCalendarList();
print_r($calendarList);
?>

8- You can now add events to the calendar, sample code:

$event = new Google_Service_Calendar_Event(array(
  'summary' => 'Test Event',
  'description' => 'Test Event',
  'start' => array(
    'dateTime' => '2018-06-02T09:00:00-07:00'
  ),
  'end' => array(
    'dateTime' => '2018-06-10T09:00:00-07:00'
  )
));

$calendarId = '[email protected]';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);

What happens here is that the event is created by the service account which is different than your own google account and has it's own data so if you didn't share the calendar with the service account and set the calendar id to primary it will create the event on the service account calendar which you can't access it normally.

I hope this helps anyone.

References:
https://stackoverflow.com/a/26067547/8702128
https://github.com/google/google-api-php-client
https://developers.google.com/calendar/quickstart/php
How to insert event to user google calendar using php?

like image 58
MomenSh Avatar answered Oct 30 '22 12:10

MomenSh