A beginner's guide to Google Api using a service account (Node.js) - Part 1

The last few days I have been working towards creating a Node.Js server that can poll and update various google apps (Calendar, Drive, Admin and Coordinate in particular), and I was a little surprised to find very little out there talking about how to do so. Even worse was there were a lot of guides or questions that seemed like they were using out of date code. So in order to log my process for myself in the future, and hopefully help someone else out, here is a step by step guide to how I managed to set it up.

Some of this information may seem pretty basic and obvious, but I'm striving to possibly help people learn instead of just providing the steps.

This first part will primarily talk about the initial set up you need to do in order to then be able to code the connection.

Google Developer

The first step requires you to go to https://console.developers.google.com and create a new project. Once you have the project set up, you will presented with something similar to this:

Google Dev

The two sections you need to visit are APIs and Credentials, which exist in the APIs & auth section.

APIs

Within the APIs page is a large list of various api you can use through google developer, this is the only place where you can enable APIs, and it is important to note that you will not be able to use them unless they enabled here. Please note which APIs you have turned on here, as you will need to list them again later.

Credentials

The credentials page allows you to create a client ID with which to connect to your google Apis. Here we will be creating the Service account by clicking the 'Create new Client ID' button and selecting the 'Service account' option.

Once the page has generated the Service account you should have a section visible on the page marked 'Service Account'. Make sure you copy the Client ID and Email address text available here. Also if the page has not automatically downloaded a .p12 file, click the 'Generate new P12 key' button and put the file away for later.

Now you should have a service account all set up, so you need to grant it permission for the apis you just enabled

Google Admin

Your next step is to visit https://admin.google.com. The follow steps seem to differ from steps followed less than a year ago, so hopefully this will help.

From the Admin console you need to visit the Security section. On the security page you need visit the 'Advanced settings' section (possibly hidden under 'Show more'). And finally on the Advance settings section you need to visit 'Manage API client access' (If there is a chance to this settings title the description should cover allowing control access to data by applications using OAuth).

The API access page should offer you two input boxes, a 'Client Name' and an 'API Scopes' box. In the Client name box you should copy the 'Client ID' created by the Service account, while in the API Scopes box you should enter all the API scopes you wish to access, seperated by a comma. 

Now finding the scopes required can be done either by using the Google APIs Discovery Service (https://developers.google.com/discovery/) or by looking at the particular API's deveroper page (for example https://developers.google.com/drive/web/scopes). For my particular project I used:

https://www.googleapis.com/auth/admin.directory.group  https://www.googleapis.com/auth/admin.directory.user  https://www.googleapis.com/auth/calendar  https://www.googleapis.com/auth/coordinate  https://www.googleapis.com/auth/drive.file 

This allows me access to the Google group, the users within it, their calendars, the google coordinate account and google drive.

Once this is all set up you should be ready to move forward with coding! Part 2 can be found here