Connecting WSO2 EI with Google Drive

dhanushka madushan
Think Integration
Published in
4 min readApr 27, 2020

WSO2 EI an open-source integration product that is widely used to integrate different services. Google Drive is a widely used cloud platform used to store data. WSO2 EI provides an inbuilt GDrive connector to interconnect services with Google Drive. In this tutorial, we will be looking in how to integrate WSO2 EI to access Google Drive.

The first step is to generate an authentication token from Google.

  • Navigate to API Credentials Page and sign in with your Google account.
  • Click Select a Project and click NEW PROJECT, to create a project.
  • Enter GDriveConnector as the name of the project and click Create.
  • Click Configure consent screen in the next screen.
  • Set User Type to External and click on Create button.

Provide the Application Name as GDriveConnector in the Consent Screen.

  • Click Create credentials and click OAuth client ID.
  • Enter the following details in the Create OAuth client ID screen and click Create.

Application type: Web Application

Name: GDriveConnector

Authorized redirect URIs: https://developers.google.com/oauthplayground

It will be provided a client id and a client secret. Keep them saved.

  • Click Library on the side menu, search for Google Drive API, and click on it.
  • Click Enable to enable the Google Drive API.

Obtaining Access Token and Refresh Token

  • Navigate to OAuth 2.0 Playground and click OAuth 2.0 Configuration button in the Right top corner.
  • Select Use your own OAuth credentials, and provide the obtained Client ID and Client Secret values as above click on Close.
  • Under Step 1, select Drive API v3 from the list of APIs, select all the scopes
  • Click on Authorize APIs button.
  • Under Step 2, click Exchange authorization code for tokens to generate a display the Access Token and Refresh Token.

You can use these tokens to access Google Drive by using GDrive connector.

Creating API to download image from GDrive.

Here, below sample proxy service that downloads a file from the google drive.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="GetFile"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<googledriverest.init>
<refreshToken>***************</refreshToken>
<clientId>*************</clientId>
<clientSecret>*************</clientSecret>
<accessToken>***********</accessToken>
<apiUrl>https://www.googleapis.com</apiUrl>
</googledriverest.init>
<googledriverest.getFile>
<fileId>*********</fileId>
<alt>media</alt>
</googledriverest.getFile>
<respond/>
</inSequence>
</target>
<description/>
</proxy>

Replace the refresh token, clientId, client secrets and access token with the secret value taken from the google. Give the fileID which is the file id that needs to retrieve.

The message builder/formatter should be changed according to the file type you retrieved. For example, to retrieve the image file, add the following message builder and formatted into the <EI_HOME>/conf/axis2/axis2.xml file.

<messageBuilder contentType="image/jpeg"
class="org.wso2.carbon.relay.BinaryRelayBuilder"/>
<messageFormatter contentType="image/jpeg"
class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>

--

--