Overview
The following diagram illustrates the use case for the Google Spreadsheet connector.
- Get the account details from the Salesforce using query operation.
- Add the account details into the Spreadsheet using the addRowsColumnsData operation.
Environment setup for running the Google Spread Sheet use cases
- Download ESB 5.0.0 from here.
- Upload the following connectors to the ESB:
- Follow the instructions given in the developer guide of the above connectors.
- Import the certificate into the ESB client keystore to use the Salesforce REST connector.Refer this link to import the certificate.
- Creating the credentials to use the Google Spreadsheet 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 Google Spreadsheet API documentation for details on creating the Client ID and Client Secret.
- Click on the Library on the side menu, and select Google Sheets 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 Google Sheets API v4 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 and display the access token and refresh token.
- Creating the credentials to use the Salesforce REST Connector:
- First, create a developer account in Salesforce using https://developer.salesforce.com/signup
- After, you need to create a connected app to get the clientId, clientSecret for getting the accessToken and refreshToken to access the Salesforce API.
If you have a Developer Edition account, log in to salesforce.com with your developer credentials, navigate to Setup ➤ Create ➤ Apps, and in the Connected Apps section, click New to create a new connected App.Then, Provide the App name, callback URL, Check Enable OAuth Settings to expand the API section and select the OAuth Scopes as below.From these steps, you will get the Consumer key(Client ID) and Consumer Secret(Client Secret) for your app. - Copy the following URL and paste it into a web browser URL bar and you have to replace the <your_client_id> and <your_redirect_uri> with the created app values. Then, you need to approve the application to access your salesforce account.
https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<your_client_id>&redirect_uri=<your_redirect_uri>
- After, successful authorization, the user's browser is redirected back to the redirect URI at the client application, with a URL of the form:
https://www.yahoo.com/?code=aPrxqJ8A8kLOza.tLq5SDTDZbLiQ.tRsRLaXCIN0DsvDG5VK6F7_T6XDsDtRhHuchcJASkBCUA%3D%3D
The client application can now extract the authorization code from its URL parameter
.For that you just copy the below URL with the respected values for the application that created above
. You can set format as urlencoded, json or xml to getting the response as the specified format option.From the above response, you can get the accessToken to access the salesforce organization via the created app and refreshToken to refresh the accessToken when it will be expired.https://login.salesforce.com/services/oauth2/token?code=aPrxqJ8A8kLOza.tLq5SDTDZbLiQ.tRsRLaXCIN0DsvDG5VK6F7_T6XDsDtRhHuchcJASkBCUA%3D%3D
==&grant_type=authorization_code&client_id=<your_client_id>&client_secret=<your_client_secret>&redirect_uri=<your_redirect_uri>&format=xml
Sample configuration
Following is a sample proxy service that illustrates how to test the above scenario.
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="ImportDatatoSpreadsheet" startOnLoad="true" statistics="disable" trace="disable" transports="http,https"> <target> <inSequence> <property expression="json-eval($.salesforceRESTHostName)" name="salesforceREST.hostName"/> <property expression="json-eval($.salesforceRESTRefreshToken)" name="salesforceREST.refreshToken"/> <property expression="json-eval($.salesforceRESTClientSecret)" name="salesforceREST.clientSecret"/> <property expression="json-eval($.salesforceRESTClientId)" name="salesforceREST.clientId"/> <property expression="json-eval($.salesforceRESTAccessToken)" name="salesforceREST.accessToken"/> <property expression="json-eval($.salesforceRESTQueryString)" name="salesforceREST.queryString"/> <property expression="json-eval($.salesforceRESTApiVersion)" name="salesforceREST.apiVersion"/> <property expression="json-eval($.salesforceRESTApiUrl)" name="salesforceREST.apiUrl"/> <property expression="json-eval($.salesforceRESTIntervalTime)" name="salesforceREST.intervalTime"/> <property expression="json-eval($.salesforceRESTRegistryPath)" name="salesforceREST.registryPath"/> <property expression="json-eval($.googlespreadsheetAccessToken)" name="googlespreadsheet.accessToken"/> <property expression="json-eval($.googlespreadsheetRefreshToken)" name="googlespreadsheet.refreshToken"/> <property expression="json-eval($.googlespreadsheetClientId)" name="googlespreadsheet.clientId"/> <property expression="json-eval($.googlespreadsheetClientSecret)" name="googlespreadsheet.clientSecret"/> <property expression="json-eval($.googlespreadsheetApiUrl)" name="googlespreadsheet.apiUrl"/> <property expression="json-eval($.spreadsheetId)" name="googlespreadsheet.spreadsheetId"/> <property expression="json-eval($.range)" name="googlespreadsheet.range"/> <property expression="json-eval($.valueInputOption)" name="googlespreadsheet.valueInputOption"/> <property name="fields" scope="operation" value="["/> <property name="length" scope="operation" value=""/> <salesforcerest.init> <apiVersion>{$ctx:salesforceREST.apiVersion}</apiVersion> <hostName>{$ctx:salesforceREST.hostName}</hostName> <accessToken>{$ctx:salesforceREST.accessToken}</accessToken> <refreshToken>{$ctx:salesforceREST.refreshToken}</refreshToken> <clientSecret>{$ctx:salesforceREST.clientSecret}</clientSecret> <clientId>{$ctx:salesforceREST.clientId}</clientId> <apiUrl>{$ctx:salesforceREST.apiUrl}</apiUrl> <intervalTime>{$ctx:salesforceREST.intervalTime}</intervalTime> <registryPath>{$ctx:salesforceREST.registryPath}</registryPath> </salesforcerest.init> <salesforcerest.query> <queryString>{$ctx:salesforceREST.queryString}</queryString> </salesforcerest.query> <property expression="//records" name="records"/> <iterate continueParent="true" description="" expression="//records" id="records_iterator" sequential="true"> <target> <sequence> <property expression="json-eval($.records)" name="records"/> <property expression="get-property('operation','fields')" name="fields"/> <script language="js"> var records = mc.getProperty("records"); var obj = eval ("(" + records + ")"); var ans; var fields = mc.getProperty("fields"); fields = fields.concat("["); var Id = obj['Id']; var Name = obj['Name']; if(Id !="" && Id !=null && Name !="" && Name !=null){ fields = fields.concat('"' + Id + '","' + Name + '"'); } fields = fields.concat("],"); mc.setProperty("fields",fields); mc.setProperty("length",fields.length()); </script> <property expression="get-property('fields')" name="fields" scope="operation"/> <property expression="get-property('length')" name="length" scope="operation"/> </sequence> </target> </iterate> <property expression="get-property('operation','fields')" name="fields" scope="operation"/> <property expression="get-property('operation','length')" name="length" scope="operation"/> <property expression="fn:concat(fn:substring(get-property('operation','fields'),0,get-property('operation','length')),']')" name="values"/> <googlespreadsheet.init> <accessToken>{$ctx:googlespreadsheet.accessToken}</accessToken> <refreshToken>{$ctx:googlespreadsheet.refreshToken}</refreshToken> <clientId>{$ctx:googlespreadsheet.clientId}</clientId> <clientSecret>{$ctx:googlespreadsheet.clientSecret}</clientSecret> <apiUrl>{$ctx:googlespreadsheet.apiUrl}</apiUrl> </googlespreadsheet.init> <googlespreadsheet.addRowsColumnsData> <spreadsheetId>{$ctx:googlespreadsheet.spreadsheetId}</spreadsheetId> <range>{$ctx:googlespreadsheet.range}</range> <valueInputOption>{$ctx:googlespreadsheet.valueInputOption}</valueInputOption> <values>{$ctx:values}</values> </googlespreadsheet.addRowsColumnsData> <property expression="$axis2:HTTP_SC" name="addRowsColumnsDataStatusCode"/> <filter regex="true" source="get-property('addRowsColumnsDataStatusCode') != 200"> <then> <log level="full"/> <property name="message" value="Unable to Insert the row "/> <loopback/> </then> <else> <log level="full"/> <property name="message" value="Success: Successfully Import the Data"/> <loopback/> </else> </filter> <respond/> <send/> </inSequence> <outSequence> <property name="messageType" scope="axis2" value="application/json"/> <payloadFactory media-type="json"> <format> { "Response":{ "process":"ImportDataIntoSpreadsheet", "activityResponse":"$1" } } </format> <args> <arg evaluator="xml" expression="get-property('message')"/> </args> </payloadFactory> <log/> <send/> </outSequence> </target> <description/> </proxy>
Sample request
{ "googlespreadsheetAccessToken": "ya29.Ci-PA9JhMsGT5GL_wsbnI8pkoEkLja16d4ms1ugiobKGIC7dqAi9BCnL8JOd6uicUg", "googlespreadsheetRefreshToken": "1/x4NQS273s3ISPcaTWJYz30nkyB-Qi_atiy4x7GupjMg", "googlespreadsheetClientId": "219479486681-r9j113m7nan5nbf955if8e4are9o7mtj.apps.googleusercontent.com", "googlespreadsheetClientSecret": "CArehV1yFnZi2J-9UNGuW9TE", "googlespreadsheetApiUrl": "https://sheets.googleapis.com/v4/spreadsheets", "spreadsheetId": "14PJALKcIXLr75rJWXlHhVjOt7z0Nby7AvcKXJGhMN2s", "range":"Expenses!A1", "valueInputOption":"RAW", "salesforceRESTHostName": "https://login.salesforce.com", "salesforceRESTRefreshToken": "5Aep861TSESvWeug_xCxj8yFaTjeV0kXa8X.gg5d1ipArZn7qCfwD9IORz9LedLqQ.HtkAr8fzUCzKKFlswxA_z", "salesforceRESTClientSecret": "3543063387981089204", "salesforceRESTClientId": "3MVG9ZL0ppGP5UrDt0n2ptTJ_fWBXDlYYXTic9.ApotX3azPQhMX2dw_gXGQ1JyPwTLWjrnCB.0K3vpUJV3Jv", "salesforceRESTAccessToken": "00D28000000avoo!ARoAQI4rBfsxeB8OLh97Ebje7Ry0DpFXbP1wWyYUKuEWTrU70lzL2zOYXopgq6YB_ZgZz.JrSaVCC2b39mGSOTR7XZGiwCVe", "salesforceRESTQueryString": "select id, name from Account limit 3", "salesforceRESTApiVersion": "v32.0", "salesforceRESTApiUrl": "https://ap2.salesforce.com", "salesforceRESTIntervalTime": "2400000", "salesforceRESTRegistryPath": "connectors/salesforcerest" }
No comments:
Post a Comment