Let’s reply to all the unread mails through a simple GET request.

Isuru Uyanage
Think Integration
Published in
7 min readFeb 28, 2020

Using WSO2 Gmail Connector we can achieve the above the use case just with a simple GET request.

The Scenario

This scenario contains a service that can be invoked through an HTTP GET request. Once the service is invoked, it returns the contents of unread emails in the Inbox under the label ‘customers’, while sending an automated response to the customers, thanking them for their feedback.

Configuring the Gmail API

Obtaining Client ID and Client Secret

  1. Navigate to API Credentials Page and sign in with your Google account.
  2. Click Select a Project and click NEW PROJECT, to create a project.

3. Enter GmailConnector as the name of the project and click Create.

4. Click Configure consent screen in the next screen.

5. Provide the Application Name as GmailConnector in the Consent Screen.

6. Click Create credentials and click OAuth client ID.

7. Enter the following details in the Create OAuth client ID screen and click Create.

Application type: Web Application

Name: GmailConnector

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

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

9. Click Library on the side menu, search for Gmail API and click on it.

10. Click Enable to enable the Gmail API.

Obtaining Access Token and Refresh Token

  1. Navigate to OAuth 2.0 Playground and click OAuth 2.0 Configuration button in the Right top corner.

2. Select Use your own OAuth credentials, and provide the obtained Client ID and Client Secret values as above click on Close.

3. Under Step 1, select Gmail API v1 from the list of APIs, select all the scopes expect the https://www.googleapis.com/auth/gmail.metadata scope.

4. Click on Authorize APIs button.

In this step if you get redirected to a Error Page-404, please do the following steps.

Go to Credentials tab in API Credentials Page under the project created, and under OAuth 2.0 Client IDs, select the Web Application Project that you have created.

Under Authorised Redirect URIs, it may not have added. Therefore, add the Redirect URI as below, and Save.

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

5. You can redo the step 4 in order to obtain the Access Token and Refresh Token by clicking on Authorize APIs button.

6. Select your gmail account if asked and allow the scopes.

7. Under Step 2, click Exchange authorization code for tokens to generate an display the Access Token and Refresh Token.

Now we are done with configuring the Gmail API.

Creating the configuration using WSO2 Integration Studio

Importing Gmail Conenctor to WSO2 Integration Studio

  1. Open WSO2 Integration Studio and create an ESB Solution Project.

2. Right click on the project that you created and click on Add or Remove Connector -> Add Connector. You are directed to the WSO2 Connector Store.

3. Search for the Gmail Connector and download it to the workspace.

4. Click Finish, and your ESB Solution Project is ready. The downloaded connector is displayed on the left side palette.

5. You can drag and drop the operations to the design canvas and build your integration logic.

6. Right click on the Solution Project you created in step1, add a Rest API.

7. Copy the following configuration by navigating to the source view.

The design view will be as below.

In the init method, you need to provide client id, client, secret, access token, refresh token that you have already obtained above.

Then it lists all the unread emails under the label name ‘customers’. According to the count it will do an iterative task which in described in the ‘reply’ sequence.

You need to create this reply sequence as the same way that you created the REST API. Then it will be displayed under the Defined Sequences.

In this sequence according to the obtained message ID, it will read the message and obtain the sender’s email address.

Then it will send a thank you email to the sender with the message body, ‘Thank you for your valuable feedback”.

This will iteratively done for the all the unread messages under the label of ‘Customers’.

Exporting Artifacts

  1. Go to File -> New -> Other -> WSO2 -> Extensions -> Project Types-> Connector Exporter Project

2. Enter a name for the Connector Exporter Project.

3. In the next screen select, Specify the parent from workspace and select the specific ESB Solution Project you created from the dropdown.

4. Now you need to add the Connector to Connector Exporter Project that you just created. Right click on the Connector Exporter Project and select, New -> Add Remove Connectors -> Add Connector -> Add from Workspace -> Connector

5. Once you are directed to the workspace, it displays all the connectors that exist in the workspace. You can select the relevant connector and click OK.

Exporting the Composite Application Project

  1. Right click on Composite Application Project and click on Export Composite Application Project.

2. Select an Export Destination where you want to save the .car file.

3. In the next Create a deployable CAR file screen, select the both created ESB Solution Project and the Connector Exporter Project to save as below and click Finish.

Deployment

Deploying on WSO2 Enterprise Integrator 7

You can copy the carbon application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps folder and start the server. Micro Integrator will be started and the composite application will be deployed. You can further refer to the application deployed through the CLI tool. You can download the CLI tool from [here](https://wso2.com/integration/micro-integrator/) from the `Other Resources` section. Make sure you first export the PATH as below.

$ export PATH=/path/to/mi/cli/directory/bin:$PATH

1. Log in to Micro Integrator using the following command.

./mi remote login

2. Provide default credentials admin for both username and password.

3. In order to view the APIs deployed, execute the following command.

./mi api show

Deploying on WSO2 Enterprise Integrator 6

1. You can copy the carbon application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps folder and start the server.

2. WSO2 EI server starts and you can login to the Management Console https://localhost:9443/carbon/ URL. Provide login credentials. The default credentials will be admin/admin.

3. You can see that the API is deployed under the API section.

Testing

Invoke the API as shown below using the curl command. Curl Application can be downloaded from here.

curl -H "Content-Type: application/json" --request GET http://localhost:8290/sendmails

The senders should receive an email with a subject of ‘Best of Europe — 6 Countries in 9 Days’, and a body of ‘Thank you for your valuable feedback.

--

--