This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Using the Gmail Connector
In this tutorial, you use the Gmail connector to send an email containing the response received from SettlePaymentEP in the previous tutorial.
See the following topics for a description of the concepts that you need to know when creating WSO2 EI artifacts:
Before you begin,
- Install Oracle Java SE Development Kit (JDK) version 1.8.* and set the JAVA_HOME environment variable.
- Download the WSO2 EI ZIP file from here, and then extract the ZIP file.
The path to this folder will be referred to as<EI_HOME>
through out this tutorial. - Select and download the relevant EI tooling ZIP file based on your operating system from here and then extract the ZIP file.
The path to this folder will be referred to as<EI_TOOLING>
through out this tutorial.For more detailed installation instructions, see the Installing Enterprise Integrator Tooling. - Open the ESB Tooling environment and click File -> Import. Then, select Existing WSO2 Projects into workspace under the WSO2 category, click Next and upload the extracted pre-packaged C-App project. This C-App contains the configurations of the previous tutorial so that you do not have to repeat those steps.
Let's get started!
Creating the credentials for using the using the Gmail Connector
Creating a Client ID and Client Secret
- As the email sender, navigate to the URL https://console.developers.google.com/projectselector/apis/credentials and log in to your google account.
If you do not already have a project, create a new project and navigate to Create Credential -> OAuth client ID.
At this point, if the consent screen name is not provided, you will be prompted to do so.
Select the Web Application option and create a client. Provide https://developers.google.com/oauthplayground as the redirect URL under Authorized redirect URIs and click on Create. The client ID and client secret will then be displayed.
See Gmail API documentation for details on creating the Client ID and Client Secret.
Click on the Library on the side menu, and select Gmail API. Click enable.
Obtaining the Access Token and Refresh Token
Follow these steps to automatically refresh the expired token when connecting to Google API:
- Navigate to the URL https://developers.google.com/oauthplayground and click on the gear wheel at the top right corner of the screen and select the option Use your own OAuth credentials. Provide the client ID and client secret you previously created and click on Close.
- Now under Step 1, select Gmail API v1 from the list of APIs and check all the scopes listed down and click on Authorize APIs. You will then be prompted to allow permission, click on Allow.
- In Step 2, click on Exchange authorization code for tokens to generate an display the access token and refresh token.
Importing the Gmail Connector into EI-Tooling
- Right click on Sample Services in the Project Explorer and Select Add or Remove Connector. Then select Add Connector and click Next.
- Select Connector Store location and click on Connect to connect to WSO2 Connector store and scroll down and select Gmail from the list of connectors.
Click Finish.
The connector is now downloaded into your Tooling environment and the connector operations will be available in the Gmail Connector palette.
Let's use these connector operations in the configuration.
Creating the Deployable Artifacts
The connector operations are used in the PaymentRequestProcessingSequence. Select this sequence, and do the following updates:
Add a Property Mediator just before the Call mediator to retrieve and store the patient's email address.
With the Property mediator selected, access the Properties tab of the mediator and fill in the information in the following table:
Field Value Property Name Select New Property New Property Name email_id Propety Action Select Set Value Type Select Expression Value Expression json-eval($.patient.email) Description Get Email Id Add another Property mediator just after the Log mediator to retrieve and store the response sent from SettlePaymentEP. This will be used within the body of the email.
With the Property mediator selected, access the Properties tab and fill in the information in the following table:
Field Value Property Name Select New Property New Property Name payment_response Property Action Select Set Value Type Select Expression Value Expression json-eval($.) Description Get Payment Response Drag and drop the init method from the Gmail Connector palette adjoining the Property mediator you added in the previous step.
With the init method selected, access the Properties tab and fill in the information in the following table:
Field Value Parameter Editor Type Select inline userId The sender's email address accessToken The access token you obtained apiUrl https://www.googleapis.com/gmail clientId The client ID you created. clientSecret The client secret you created. refreshToken The refresh token you obtained. Add the sendMail method from the Gmail Conector palette and access the Properties tab and fill in the information in the following table:
Field Value Description Parameter Editor Type Select inline to {$ctx:email_id} Retreives the patient email address that was stored in the relevant Property mediator. subject Payment Status The subject line in the email that is sent out. messageBody {$ctx:payment_response} Retreives the payment response that was stored in the relevant Property mediator.
The updated PaymentRequestProcessingSequence should now look like this:
- Save the updated sequence configuration.
- Right click on SampleServicesConnectorExporter and navigate to New -> Add/Remove Connectors and select Add connector and click on Next. Select Workspace to list down the connectors that were added.
Select the Gmail connector from the list and click OK and then Finish.
Deploying the Artifacts to WSO2 EI
Since you added a connector to the Conector Exporter Project, this will need to be packaged into our existingCApp.
Package the C-App names SampleServicesCompositeApplication project with the artifacts created.
Ensure the following artifact check boxes are selected in the Composite Application Project POM Editor.
- SampleServices
- HealthcareAPI
- ClemencyEP
- GrandOakEP
- PineValleyEP
- ChannelingFeeEP
- SettlePaymentEP
- SampleServicesRegistry
- SampleServicesConnectorExporter
- SampleServices
On the Servers tab, expand the WSO2 Carbon server, right-click SampleServicesCompositeApplication, and choose Redeploy. The Console window will indicate that the CApp has been deployed successfully.
If you do not have a server added in Eclipse, refer this tutorial.
You can also deploy the artifacts to the Enterprise Integrator using a Composite Application Archive (CAR) file.
Sending requests to WSO2 EI
Create a JSON file names
request.json
with the following request payload. Make sure you provide a valid email address so that you can test the email being sent to the patient.{ "name": "John Doe", "dob": "1940-03-19", "ssn": "234-23-525", "address": "California", "phone": "8770586755", "email": "johndoe@gmail.com", "doctor": "thomas collins", "hospital": "grand oak community hospital", "cardNo": "7844481124110331" }
- Open a command line terminal and execute the following command from the location where
request.json
file you created is saved:curl -v -X POST --data @request.json http://localhost:8280/healthcare/categories/surgery/reserve --header "Content-Type:application/json"
This is derived from the URI-Template defined when creating the API resource.
http://<host>:<port>/categories/{category}/reserve
You will see the response as follows:
{"message":"Payment request successfully submitted. Payment confirmation will be sent via email."}
An email will be sent to the provided patient email address with the following details:
Subject: Payment Status Message: {"patient":"John Doe","actualFee":7000.0,"discount":20,"discounted":5600.0,"paymentID":"21d0e220-1714-4051-a4f5-d0d31fe42496","status":"Settled"}
You have now explored how to import the Gmail connector in WSO2 EI and then use the connector operation to send emails.