Wednesday, October 12, 2016

Accessing Google APIs using OAuth 2.0

Here is my use case.
I recently wanted to use WSO2 ESB GoogleSpreadsheet Connector. Before attempting any other spreadsheet operation in connector, I have to call the googlespreadsheet.init element first. This configuration authenticates with Google Spreadsheet by configuring the user credentials using OAuth2 authentication for accessing the Google account that contains the spreadsheets.

I couldn't initially get any simple answers/instructions in www on how to set up the following four elements from my Google Account. I have shared the process for your benefit hoping this will save loads of your valuable time.
1. Client ID
2. Client Secret
3. Refresh Token
4. Access Token

Steps
1. Go to https://console.developers.google.com
2. Under Credentials -> Create Credentials -> OAuth client ID

3. Select Application Type: Web Application
    Name: <appName>
   Authorized redirect URIs: https://developers.google.com/oauthplayground

4. Click Create.
Google will immediately show your Client ID and Client Secret as follows (you can get these details later also).

Following is how the edit view shows you the required information.

6. Click the settings icon.
7. Select the Use your own OAuth credentials check box
8. Fill two fields OAuth Client ID and OAuth Client secret with the values you retrieved in step 4.

9. Now in Step 1 select the Google Sheets APIv4 all 4 scopes as shown below.
10. Click on Authorize APIs
11. Once ask approve permission to access the selected scopes.


 12. In Step 2 Click on Exchange authorization code for tokens button.
13. These are your Refresh and Access Tokens

Now all four fields required are retrieved.
Following is how you will be using googlespreadsheet.init element inside your proxy/sequence configuration.

<googlespreadsheet.init>
    <accessToken>{$ctx:accessToken}</accessToken>
    <clientId>{$ctx:clientId}</clientId>
    <clientSecret>{$ctx:clientSecret}</clientSecret>
    <refreshToken>{$ctx:refreshToken}</refreshToken>
    <accessTokenRegistryPath>{$ctx:accessTokenRegistryPath}</accessTokenRegistryPath>
    <apiUrl>{$ctx:apiUrl}</apiUrl>
</googlespreadsheet.init>

Following is a sample JSON payload to invoke googlespreadsheet.init method in ESB google spreadsheet connector. Note that the registry path is optional here.

{  
  "googlespreadsheetAccessToken": "xxNN.XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX",
  "googlespreadsheetRefreshToken": "1/KSKKDKDK_Isdf_dsalkfdffafdskfdsafdksfj_sdfSWE",
  "googlespreadsheetClientId": "721330057745-aq810qmciljtvlablt1hjlnh6vjjer7j.apps.googleusercontent.com",
  "googlespreadsheetClientSecret": "0GeUJ4KK3Flk8C9S0Ua51jZS",
  "googlespreadsheetApiUrl": "https://sheets.googleapis.com"
}

Note: As of today WSO2 ESB googlespreadsheet connector is compatible with Google Spreadsheets API v3(Legacy). But for authentication purposes there will be no much of a difference. I have written my own connector for Google Spreadsheets API v4 to use v4 specific methods like spreadsheets.create
Refer [1] for more information on writing your own connector.

Reference:
[1] https://docs.wso2.com/display/ESBCONNECTORS/Writing+a+Connector

No comments:

Post a Comment