[1] Manual preparation (one-time).
This is the starting point. You need to be signed in to approach Google APIs.
Navigate to GDC (Google Development Console) at https://console.developers.google.com/project. "Create new project".
Go to GDC > APIs & Auth > APIs, scroll down and "enable" your API.
(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 '
[1] Manual preparation (one-time).
This is the starting point. You need to be signed in to approach Google APIs.
Navigate to GDC (Google Development Console) at https://console.developers.google.com/project. "Create new project".
Go to GDC > APIs & Auth > APIs, scroll down and "enable" your API.
(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.
GDC > Credentials > Download JSON.
The output will be something like this:
{
"installed": {
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"client_secret": "4i1................",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"client_email": "",
"redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "oob"],
"client_x509_cert_url": "",
"client_id": "506................apps.googleusercontent.com",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs"
}
}
[2] Get OAuth code, refresh token and access token (one-time).
- Open a browser and navigate to
https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=https://localhost&scope=https://www.googleapis.com/auth/calendar&client_id=your_client_id&access_type=offline&prompt=consent
- 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:
https://localhost/?code=4/4/cVwJ35................
- (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.)
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.
POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Content-type: application/x-www-form-urlencoded
grant_type=authorization_code&code=your_oauth_code&client_id=your_client_id&client_secret=your_client_secret&redirect_uri=https://localhost
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:
{
"access_token" : "ya29.1................",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "1/Oauh................"
}
[3] Make the API call.
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.
POST https://www.googleapis.com/oauth2/v3/token HTTP/1.1
Content-type: application/x-www-form-urlencoded
client_id=your_client_id&client_secret=your_client_secret&refresh_token=your_refresh_token&grant_type=refresh_token
The response content will be something like:
{
"access_token": "ya29.1................",
"token_type": "Bearer",
"expires_in": "3600"
}
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.
GET https://www.googleapis.com/calendar/v3/users/me/calendarList HTTP/1.1
Authorization: Bearer your_access_token
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.
GET https://www.googleapis.com/calendar/v3/calendars/your_calendar_id/events HTTP/1.1
Authorization: Bearer your_access_token
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.