Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

How to setup and use the Google Calendar API

Original – by Freek Van der Herten – 4 minute read

For a project I'm working on I needed to interact with a Google Calendar. If you've ever worked with some API's by Google then you know their documentation can be very confusing. It's not that they don't have documentation, but code examples of common use cases are simply not present. You must wade through a lot of pages to learn basic things such as how to make an authorized request let alone how to fetch calendar events. In this post I'd like to explain in a human readable way how setup and use the Google Calendar API.

Screen Shot 2016-05-09 at 19.44.40

Getting credentials

The first thing you'll need to do is to get some credentials to use Google API's. I'm assuming that you've already created a Google account and are signed in. Head over to Google API's site and click "Select a project" in the header. Choose "Create a new project" in the menu that opens. You can give that project any name you'd like.

Screen Shot 2016-05-09 at 18.55.44

Next up we must specify which API's the project may consume. In the list of available API's click "Google Calendar API". On the next screen click "Enable".

Screen Shot 2016-05-09 at 18.56.07

Now that you've created a project that has access to the Calendar API it's time to download a file with these credentials. Click "Credentials" in the sidebar. You'll want to create a "Service account key".Screen Shot 2016-05-09 at 18.56.48

On the next screen you can give the service account a name. You can name it anything you'd like. In the service account id you'll see an email address. We'll use this email address later on in this guide. Select "JSON" as the key type and click "Create" to download the JSON file.

Screen Shot 2016-05-09 at 18.57.18

Setting up the Google Calendar

Now that everything is set up on the API site, we'll need to configure some things on the Google Calendar site. Head over Google Calendar and view the settings of the calendar you want to work with via PHP. On the "Share this Calendar" tab add the service account id that was displayed when creating credentials on the API site. Screen Shot 2016-05-09 at 19.10.52

Open up the "Calendar Details" tab to see the id of the calendar. We'll need this id later on.

Screen Shot 2016-05-09 at 19.19.10

Accessing the calendar in PHP

Now that everything is set up at Google, let's finally write some PHP. In your project make sure you require `google/apiclient` in your `composer.json`. This piece of code can be used to fetch all events on your calendar: ``` $client = new Google_Client();

// $credentialsJson should contain the path to the json file downloaded from the API site $credentials = $client->loadServiceAccountJson( $credentialJson, 'https://www.googleapis.com/auth/calendar' );

$client->setAssertionCredentials($credentials);

$service = new Google_Service_Calendar($client);

// $calendarId should contain the calendar id found on the settings page of the calendar $events = $service->events->listEvents($calendarId);


In the example above all events will get fetched. Of course the API can do a whole lot more. Take a look at <a href="https://developers.google.com/google-apps/calendar/v3/reference/events">the API docs</a> to learn what's possible.
<h3>In closing</h3>
Setting up and using the calendar API, and the other Google API's is not that difficult. The key thing that's missing is proper documentation. I hope that in the future Google will add some easy to follow tutorials and practical examples to their docs. It took me about an hour to learn and set everything mentioned in this post. With good documentation the required time would be around 5 minutes (and there would be much less frustration).

If you're using Laravel you'll be happy to know that I'm currently developing <a href="https://github.com/spatie/laravel-google-calendar">a package</a> that makes working with a Google Calendar a breeze. My goal is to make this code work:

use \Spatie\GoogleCalendar\Event;

//creating events $event = new Event(); $event->title = 'My new event' $event->start = Carbon::now(); $event->end = Carbon::now()->addHour(); $event->save();

//fetching all events $allEvents = Event::all();

//updating an event $event = $allEvents->first(); $event->name = 'updated name'; $event->save();

//deleting an event $allEvents->last()->delete();


I'm not planning on supporting all the features of the native API, but if you need only the basic functionality, this will be the package for you.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "How to setup and use the Google Calendar API"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.