oAuth 2.0 with Google Calendar APIs basic example - Google Groups

3 downloads 100 Views 580KB Size Report
Developers Console. 2.3 Create a new project in. Google Console. 2.4 Enable Google Calendar API. 2.5 Generate client id
Integration​ > r​ mp > google​ >​

_Google oAuth 2.0 configuration & Calendar APIs basic example

oAuth 2.0 with Google Calendar APIs basic example

CONTENTS 1 oAuth 2.0 mechanism 2 Get oAuth 2.0 Credentials 2.1 Get the authorization_code 2.2 Connect to Google Developers Console 2.3 Create a new project in Google Console 2.4 Enable Google Calendar API 2.5 Generate client id & secret 2.6 Get authorization_code 3 Configure the RunMyProcess connectors 3.1 Generate the refresh_token 3.2 Renew access_token 3.3 Call the Google Calendar APIs 4 Configure the RunMyProcess process/CAPI

oAuth 2.0 mechanism

You will create an application (RunMyProcess based) that will try to access Google resources, on behalf of a Google user. There are 5 steps: A.  RunMyProcess sends a client_id & client_secret & scope to Google servers, that will ask the user his consent B.  If user gives his consent, Google servers return an authorization_code. The authorization_code is valid until the user revokes it (on Google side) C.  RunMyProcess will send this authorization_code to Google servers…. D.  ...that will return an access_token  and refresh_token. The refresh_token may be used to  obtain a new access_token. E.  RunMyProcess uses access_token to access a specific resource under the scope provided in (A)  A & B will be done manually by the user, once only (he’ll have to do it again he revokes the access).

Get oAuth 2.0 Credentials

1. Get the authorization_code Go to RunMyProcess, create a Project and a Webinterface to receive the authorization code:

Let’s call this webinterface url redirect_uri note: the text input variable is code

2. Connect to Google Developers Console https://console.developers.google.com/

3. Create a new project in Google Console

Enter a project name & project id then ‘Create’

4. Enable Google Calendar API

Click on ‘Enable an API’:

Search for ‘calendar’ then click on ‘OFF’ in front of ‘Calendar API’

Tick ‘I have read and agree to Calendar API Terms of Service.’ and click on ‘Accept’:

You should now see ‘Google Calendar API’ in Enabled APIs:

5. Generate client id & secret Go to APIs & auth > Credentials. Then Click on ‘Create new Client ID’

Select ‘Web Application’ then ‘Configure consent screen’: 

Select an email address and enter ‘RunMyProcess’ as product name. Then click on save:

Enter https://live.runmyprocess.com as Authorized JS origins, enter redirects_uri (see 1)) as authorized redirects uri, then click on ‘Create Client ID/Update’:

You should now see the Client ID and Secret top right: (refresh page if you don’t see it)

6. Get authorization_code Let’s build the url to retrieve the authorization_code to then request access to google calendar apis:

https://accounts.google.com/o/oauth2/auth? scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar&state=security_token%123&redirect_uri=https%3A%2F%2Flive.runmyprocess.com%2Flive% Here’s a link to url decode/encode string online : http://meyerweb.com/eric/tools/dencoder/ Do use your own security_token/client_secret, redirect_uri and client_id in the url above. Then open this url in your browser. You’ll be asked to login into google, then you’ll see:

Select Accept. You’ll be then redirected to your RunMyProcess webinterface with the authorization_code passed in the url:

This is your authorization_code.

Configure the RunMyProcess connectors Let’s focus on this part:

Every time you’ll want to call Google APIs, you’ll need to pass an access_token. You need to make sure this access_token is still valid. If it has expired,  then you need to renew it using the refresh_token. Let’s create the provider + connector to return an access_token:

1. Generate the refresh_token Create a new provider: Select authentication scheme = none

Under this provider create a new connector:

Title : Get refresh_token POST on o/oauth2/token Content: code=${authorization_code?url}&redirect_uri=${"https://live.runmyprocess.com/live/123/appli/150519?P_mode=TEST"? url}&client_id=${"123.apps.googleusercontent.com"?url}&scope=${"https://www.googleapis.com/auth/calendar"? url}&client_secret=${"Y123Bo72nz"?url}&grant_type=authorization_code Replace in the above content: redirect_uri, client_id, client_secret parameters. When you call this connector passing your authorization_code, you’ll get  as output:

{   "access_token" : "ya29.yQ123lpR5oKlA",   "token_type" : "Bearer",   "expires_in" : 3600,   "refresh_token" : "1/28B2346n8" } The refresh_token will not expire until you revoke it. The access_token will expire, and you’ll need the refresh_token to renew it.

2. Renew access_token Under the same provider ‘Google oauth2 auth:none’, create a new connector:

Title: get access_token from refresh_token POST on o/oauth2/token Content: client_id=${"87123.apps.googleusercontent.com"?url}&client_secret=${"1234A2nz"? url}&grant_type=refresh_token&refresh_token=${"1/281231zAcbWP6n8"?url} Replace in the content above: client_id, client_secret, refresh_token. This connector should give you as output param: {   "access_token" : "ya29.yQDalWy123­Q",   "token_type" : "Bearer",   "expires_in" : 3600 }

3. Call the Google Calendar APIs Create a new provider “Google APIs auth:Bearer” Authentication scheme : Custom Scheme name : Bearer Credentials ${access_token}

Under this provider, configure the connector that will retrieve all the events from a specific calendar:

GET on calendar/v3/calendars/ntriballier@runmyprocess­prem.com/events Replace the string in blue by your own calendar_id

=> as output ${P_result.items} will give you all the events in this calendar More information on Google Calendar APIs > https://developers.google.com/google­apps/calendar/v3/reference/calendarList/get

Configure the RunMyProcess process/CAPI You have to plug the 2 previous connectors to your process/CAPI :

Congratulations! you’ve performed a Google Calendar Integration with RunMyProcess using oauth2 authentication scheme.