Skip to content

Gmail Connector Example

The Gmail Connector allows you to access the Gmail REST API through WSO2 EI. Gmail is a free, Web-based e-mail service provided by Google. It allows you to send, read, and delete emails through the Gmail REST API. Furthermore, it provides the ability to read, trash, untrash, and delete threads, create, update, and delete drafts, get the Gmail profile, and access the mailbox history as well, while handling OAuth 2.0 authentication.

What you'll build

Using Gmail Connector

This example demonstrates a scenario where a customer feedback Gmail account of a company can be easily managed using the WSO2 Gmail Connector. This application 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 of Customers, while sending an automated response to the customer, thanking them for their feedback. The number of emails that can be handled in a single invocation is specified in the application.

If you do not want to configure this yourself, you can simply get the project and run it.

Configure the connector in WSO2 Integration Studio

  1. Follow these steps to set up the ESB Solution Project and the Connector Exporter Project.
  2. Open WSO2 Integration Studio and create an Integration Project. Creating a new Integration Project

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

  4. Search for the specific connector and download it to the workspace. In this case, Salesforce is used as an example, but you can do this for the connector you require for your integration scenario.
    Search Connector in the Connector Store

  5. Click Finish, and your Integration Project is ready. The downloaded connector is displayed on the left side palette with its operations.

  6. You can drag and drop the operations to the design canvas and build your integration logic. Drag connector operations

  7. Right click on the created ESB Solution Project and select, -> New -> Rest API to create the REST API. Adding a Rest API

  8. Follow these steps to configure the Gmail API and obtain the Client Id, Client Secret, Access Token and Refresh Token.

  9. Provide the API name as SendMails. You can go to the source view of the XML configuration file of the API and copy the following configuration.

    <?xml version="1.0" encoding="UTF-8"?>
    <api context="/sendmails" name="SendMails" xmlns="http://ws.apache.org/ns/synapse">
        <resource methods="GET">
            <inSequence>
                <gmail.init>
                    <userId></userId>
                    <accessToken></accessToken>
                    <apiUrl>https://www.googleapis.com/gmail</apiUrl>
                    <clientId></clientId>
                    <clientSecret></clientSecret>
                    <refreshToken></refreshToken>
                </gmail.init>
                <gmail.listAllMails>
                    <includeSpamTrash>false</includeSpamTrash>
                    <maxResults>20</maxResults>
                    <q>is:unread label:customers</q>
                </gmail.listAllMails>
                <iterate expression="json-eval($.messages)">
                    <target>
                        <sequence>
                            <sequence key="reply"/>
                        </sequence>
                    </target>
                </iterate>
                <respond/>
            </inSequence>
            <outSequence/>
            <faultSequence/>
        </resource>
    </api>

  10. Right click on the created ESB Solution Project and select New -> Sequence to create the defined sequence called reply.

  11. Provide the Sequence name as reply. You can go to the source view of the XML configuration file of the API and copy the following configuration.

    <?xml version="1.0" encoding="UTF-8"?>
    <sequence name="reply" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
        <property expression="json-eval($.id)" name="msgId" scope="default" type="STRING"/>
        <gmail.getAccessTokenFromRefreshToken>
            <clientId></clientId>
            <clientSecret></clientSecret>
            <refreshToken></refreshToken>
        </gmail.getAccessTokenFromRefreshToken>
        <gmail.readMail>
            <id>{$ctx:msgId}</id>
        </gmail.readMail>
        <property expression="json-eval($.payload.headers[6].value)" name="response" scope="default" type="STRING"/>
        <log level="custom">
            <property expression="get-property('response')" name="response1"/>
        </log>
        <gmail.getAccessTokenFromRefreshToken>
            <clientId></clientId>
            <clientSecret></clientSecret>
            <refreshToken></refreshToken>
        </gmail.getAccessTokenFromRefreshToken>
        <gmail.sendMail>
            <to>{$ctx:response}</to>
            <subject>Best of Europe - 6 Countries in 9 Days</subject>
            <from>isurumuy@gmail.com</from>
            <messageBody>Thank you for your valuable feedback.</messageBody>
        </gmail.sendMail>
    </sequence>

  12. In the Rest API and in the Sequence, provide your obtained Client ID, Client Secret, Access Token, and Refresh Token accordingly. The userID should be your Gmail address.

  13. Follow these steps to export the artifacts.

Exporting Integration Logic as a CApp

CApp (Carbon Application) is the deployable artifact on the Enterprise Integrator runtime. Let us see how we can export integration logic we developed into a CApp along with the connector.

Creating Connector Exporter Project

In order to bundle Connector into a CApp a Connector Exporter Project is needed.

  1. Navigate to File -> New -> Other -> WSO2 -> Extensions -> Project Types -> Connector Exporter Project.
    Add 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 Integration Project you created from the dropdown. Naming Connector Exporter Project

  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. Selecting Connector from Workspace

Creating a Composite Application Project

To export the Solution Project as a CApp, a Composite Application Project needs to be created. Usually, when a solution project is created, this project is automatically created by Integration Studio. If not, you can specifically create it by navigating to File -> New -> Other -> WSO2 -> Distribution -> Composite Application Project.

Exporting the Composite Application Project

  1. Right click on Composite Application Project and click on Export Composite Application Project.
    Export as a Carbon Application

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

  3. In the next Create a deployable CAR file screen, select both the created Integration Project and the Connector Exporter Project to save and click Finish. The CApp will get created at the specified location provided at the previous step. Create a deployable CAR file

Get the project

You can download the ZIP file and extract the contents to get the project code.

Download ZIP

Deployment

Follow these steps to deploy the exported CApp in the Enterprise Integrator Runtime. Deploying on WSO2 Enterprise Integrator 7

You can copy the composite application to the /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 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
Click here for instructions on deploying on WSO2 Enterprise Integrator 6
  1. You can copy the composite application to the /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."

What's Next

Top