Google Calendar API: Connect using Node.js and an API key

Sometimes working out which API method to use to connect to the Google APIs can be a bit confusing — let me help.

There are 3 methods to connect to the Google APIs:

  • A service account with a JSON or P12 private key
  • OAuth

But which method to use?

  • Generally you would use the API key method to consume a Google API which provides you with a consumable service, for example the Google Maps API which provides you with Google Maps data,
  • OAuth enables anyone to give you some level of access to their data, for example a calendar SaaS app that integrates with your Google Calendar and asks for your permission to manage your calendar,
  • A service account would be used by a Sysadmin or Developer to take full control of a G Suite Domain’s user data, for example to give a business application full control of all users calendars to add and remove calendar events behind the scenes.

What the documentation doesn’t tell you

The example below uses the googleapis library to connect to the Google Calendar API and get freebusy information for a calendar between 3 pm and 4 pm the next day. You could very easily adapt this code to suit your own purposes and to connect to a different Google API — the method is exactly the same.

Let’s get started

You are going to need:

  • A Google Calendar with its sharing settings set so that freebusy information is publicly available
  • A Node.js runtime environment (your local machine, AWS Lambda, Firebase Functions, your server, etc)

Make sure to update your api key and the calendar you want to query.

--

--

More about me at https://jasondark.co.nz

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store