Connecting Google Spreadsheet APIs With Mule ESB
Google's API lets you communicate with their integration services. Learn to configure and connect Mule ESB with Google API, with code and a video tutorial.
Join the DZone community and get the full member experience.
Join For FreeGoogle provides an Application Programming Interface which allows you to communicate with Google services and their integration services. It is useful for a third-party to take advantage of APIs and can even extend the functionalities of APIs. Examples of these include Search, YouTube, Google Spreadsheet, Google Maps, Google Contacts, etc. Google APIs require authentication and authorization using OAuth 2.0.
Mule ESB has capabilities to connect Google APIs and it provides various connectors to connect the Google APIs and perform the operations that you need. Below is a list of connectors available in Anypoint Exchange to connect Google APIs.
Google Spreadsheets Connector.
Google Contacts Connector.
Google Tasks Connector.
Google Calendar Connector.
Google Prediction Connector.
Google Contacts RAML.
Google Drive RAML.
In this article, you will see Google Spreadsheets Connector. Google Spreadsheets Connector provides instant API connectivity to Google Spreadsheets APIs, which allows you to create, modify, or access Google docs using OAuth 2.0 authentication. For more details on Google Spreadsheets API, click here.
By default, you will not find Google Spreadsheets Connector in your Anypoint Studio, so you can install the connector from Anypoint Exchange.
The first thing you need to do is authorize the operation to generate the token.
<google-spreadsheets:config-with-oauth name="Google_Spreadsheets" consumerKey="${gs.clientid}" consumerSecret="${gs.clientsecret}" doc:name="Google Spreadsheets" scope="https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://spreadsheets.google.com/feeds https://docs.google.com/feeds https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email">
<google-spreadsheets:oauth-callback-config domain="${oauth.host}" path="oauth2callback" async="true"/>
</google-spreadsheets:config-with-oauth>
Fields | Description |
consumerKey | The OAuth consumer key. It can obtain from the Google developer console. |
consumerSecret | The OAuth consumer secret. It can obtain from the Google developer console. |
scope | https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://spreadsheets.google.com/feeds https://docs.google.com/feeds https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/spreadsheets |
Access Token URL | The URL defined by the Service Provider to obtain an access token. Value: - https://accounts.google.com/o/oauth2/token |
Authorization URL | The URL defined by the Service Provider where the resource owner will be redirected to grant authorization to the connector. Value: - https://accounts.google.com/o/oauth2/auth |
Callback URL | It is made up of the domain, local port, remote port, and path. Example: http://localhost:3000/oauthcallback. This URL also needs to add under redirect URIs in the Google developer console. |
Configuration Reference
Authorizing the operation will return tokenID, and it can be read using flow variable #[flowVars['OAuthAccessTokenId']]
Once authorization is successful, you can use any operation to be performed depending on your requirement; make sure you are sending the token with every request to Google Spreadsheets APIs.
Sometimes there can be a case when you need to use the authorized operation in one flow and another operation in other flow. In such a case, you need to use object store to store the tokenID generated during authorize operation to retrieve from the object store in other flow.
Code
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:ws="http://www.mulesoft.org/schema/mule/ws" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:context="http://www.springframework.org/schema/context" xmlns:google-spreadsheets="http://www.mulesoft.org/schema/mule/google-spreadsheets" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/google-spreaadsheets http://www.mulesoft.org/schema/mule/google-spreadsheets/current/mule-google-spreadsheets.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/google-spreadsheets http://www.mulesoft.org/schema/mule/google-spreadsheets/current/mule-google-spreadsheets.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<google-spreadsheets:config-with-oauth name="Google_Spreadsheets" consumerKey="${gs.clientid}" consumerSecret="${gs.clientsecret}" doc:name="Google Spreadsheets" scope="https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://spreadsheets.google.com/feeds https://docs.google.com/feeds https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email">
<google-spreadsheets:oauth-callback-config domain="${oauth.host}" path="oauth2callback" async="true"/>
</google-spreadsheets:config-with-oauth>a
<flow name="google-spreadsheet-apiFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/authorize" doc:name="HTTP" allowedMethods="GET"/>
<google-spreadsheets:authorize config-ref="Google_Spreadsheets" doc:name="Google Spreadsheets" accessTokenUrl=" https://accounts.google.com/o/oauth2/token" authorizationUrl="https://accounts.google.com/o/oauth2/auth"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<google-spreadsheets:get-all-cells-as-csv config-ref="Google_Spreadsheets" lineSeparator="|" spreadsheet="Employee" worksheet="Employee" accessTokenId="#[flowVars['OAuthAccessTokenId']]" doc:name="Google Spreadsheets"/>
<logger message="data fetched. #[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
References
- Google API Commons: https://github.com/mulesoft/google-api-commons
- Google Calendar: https://github.com/mulesoft/google-calendar-connector
- Google Contacts: https://github.com/mulesoft/google-contacts-connector
- Google Cloud Messaging: https://github.com/mulesoft/google-cloud-messaging-connector
- Google Drive: https://github.com/mulesoft/google-drive-connector
- Gmail: https://github.com/mulesoft/gmail-connector
- Google Prediction: https://github.com/mulesoft/google-prediction-connector
- Google Spreadsheets: https://github.com/mulesoft/google-spreadsheets-connector
- Google Tasks: https://github.com/mulesoft/google-tasks-connector
Now, you know how to connect Google APIs With Mule ESB.
Here is the video tutorial:
Opinions expressed by DZone contributors are their own.
Comments