Steps to make a Google API call


This short summary of steps to prepare and realize a Google API call is based on invoking the Google Calendar API, but other segments should be quite similar.

[1] Manual preparation (one-time).

[1a] Sign in to Google.

This is the starting point. You need to be signed in to approach Google APIs.

[1b] Create a project.

Navigate to GDC (Google Development Console) at https://console.developers.google.com/project. "Create new project".

[1c] Enable the API you are going to invoke.

Go to GDC > APIs & Auth > APIs, scroll down and "enable" your API.

[1d] Create new Client ID.

(We assume here that we need to access private data; accessing public data would be simpler - no client ID but a simple API key would be used, but this is not subject to this study).

GDC > Credentials > Create new Client ID > [select 'Installed Application'] > ... Credentials > [Edit the Client ID] > [Enter https://localhost for '

This short summary of steps to prepare and realize a Google API call is based on invoking the Google Calendar API, but other segments should be quite similar.

[1] Manual preparation (one-time).

[1a] Sign in to Google.

This is the starting point. You need to be signed in to approach Google APIs.

[1b] Create a project.

Navigate to GDC (Google Development Console) at https://console.developers.google.com/project. "Create new project".

[1c] Enable the API you are going to invoke.

Go to GDC > APIs & Auth > APIs, scroll down and "enable" your API.

[1d] Create new Client ID.

(We assume here that we need to access private data; accessing public data would be simpler - no client ID but a simple API key would be used, but this is not subject to this study).

GDC > Credentials > Create new OAuth Client ID > ... Edit the Client ID adding https://localhost to the 'Authorized JavaScript origins' and 'Authorized redirect URIs' sections.

Now you see the 'Client ID for native application' data: CLIENT ID, CLIENT SECRET, REDIRECT URIs.

[1e] Optionally export OAuth basic data (to have it at hand for the coming actions).

GDC > Credentials > Download JSON.

The output will be something like this:

Now have a short rest before starting with the programmatic calls to follow.

[2] Get OAuth code, refresh token and access token (one-time).

[2a] Get OAuth code.
  • Open a browser and navigate to
    where your_client_id is the CLIENT ID from 'GDC > APIs & Auth > Credentials' (or the client_id taken from the earlier exported json),
  • Sign in when asked to do so,
  • Press the 'Accept' button on the newly opened page requesting your consent for your product (as entered earlier on the 'GDC > APIs & Auth > Consent screen') to access or manage your calendar,
  • Watch the address bar - now you have received your OAuth code as part of the URL like this:
    (As far as above URL starts with 'localhost' page content does not matter, never mind even if showing some error.)

    Store the code (without the possible # trailer) for using it with next actions. (This code can be further used only once, but you can freely browse for a new code when needed.)

[2b] Get refresh token and access token.

Now an HTTP POST request has to be sent. Following sample is based on the popular Fiddler Composer, but you may prefer to use cURL or help yourself with a HTML form or a javascript/jQuery request.

where your_auth_code is the above received OAuth code, your_client_id is the CLIENT ID from 'GDC > APIs & Auth > Credentials' (or the client_id taken from the earlier exported json) and your_client_secret is the CLIENT SECRET from 'GDC > APIs & Auth > Credentials' (or the client_secret taken from the earlier exported json).

NOTE that grant_type is the FIRST parameter in the list.
NOTE that the OAuth code can be used only once, so, you will have to navigate each time for a new OAuth code, as explained earlier, if current POST request fails for any reason.

Well, your goal is to get an HTTP 200 response code. The response content will be something like:

Here are the desired refresh token and access token (without the quotes). Note and keep save especially the refresh code. The refresh code will be used each time when starting a new API call, where an access token is needed and the short living access code has meanwhile expired.

[3] Make the API call.

[3a] Get new access token (unless your latest access token has not yet expired).

Again an HTTP POST request has to be sent. Following sample is based on the popular Fiddler Composer, but you may prefer to use cURL or help yourself with a HTML form or a javascript/jQuery request.

where your_client_id is the CLIENT ID from 'GDC > APIs & Auth > Credentials' (or the client_id taken from the earlier exported json), your_client_secret is the CLIENT SECRET from 'GDC > APIs & Auth > Credentials' (or the client_secret taken from the earlier exported json) and your_refresh_token is the refresh_token (without the quotes) received as a result of the previous post request.

The response content will be something like:

We have a fresh access token and can finally make the API calls.
[3b] Call that Google API (Google Calendar API in this case):

We make HTTP GET requests. As earlier stated, the samples are based on the popular Fiddler Composer, but you may prefer to use cURL or help yourself with a HTML form or a javascript/jQuery request.

Sample 1: Get your calendars list.
where your_access_token is above access_token (without the quotes).

We get a JSON response bearing a list of the various calendars for the Google profile in question, where we can get info about the calendar name/summary, calendar ID etc. The calendar ID is specifically important, because we need it for the call for the single calendar entries list.

Sample 2: Get single calendar entries list.
where your_calendar_id is the calendar ID got from the results of the previous API call, requesting calendars list, and your_access_token is above access_token (without the quotes).

We now have certain calendar properties and a list of the calendar events with indications about name, start date, default reminders, override reminders, visibility, web address etc. Of course, there are very many filtering arguments you can add to your request - read more in the relevant API reference online.

That's it. Not easy the first time, but efforts are paying off later - you have a stable and easy fine-grained access to the universe of Google data.


Back to List