Using Salesforce REST API Connector with WSO2 Micro Integrator
In this blog post, let’s look at how to use the Salesforce REST API connector to invoke set of operations.
Obtaining OAuth 2tokens from Salesforce
First, we will create a Salesforce App and obtain the OAuth 2tokens from Salesforce REST API.
You can create a Salesforce account (developer edition) and create a connected App.
- Log in to Salesforce after verifying your account, with the newly created credentials. In the upper-right corner, select Setup.
- Then navigate to Apps > App Manager.
- Click on the New Connected App.
- On the New Connected App page, fill the required fields, which are Connected App Name, API Name and Contact Email under Basic Information:
- Go to API (Enable OAuth Settings), and select Enable OAuth Settings. In the Callback URL field, enter https://login.salesforce.com. In the Selected OAuth Scopes field, select Access and manage your data (API), Perform requests on your behalf at any time (refresh_token, offline_access), Provide access to your data via the Web (web), and then click Add.
- Click the Save button to save the new Connected App.
- Now navigate to Connected Apps (Apps > App Manager) list, and click the App that you have just created, and then click on Manage.
8. On the page that opens, click the Edit Policies button. Under OAuth policies, select All users may self-authorize in the Permitted Users list, and then click the Save button.
10. Now navigate to Connected Apps (Apps > App Manager) list, and click the App that you have just created, and then click on View.
11. In API (Enable OAuth Settings), and note down the Consumer Key and Consumer Secret.
9. Now we have to obtain the access token and refresh token as below. Enter the following URL in your web browser.
https://<INSTANCE>.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<CONSUMER_KEY>&redirect_uri=https://login.salesforce.com
If this is a new browser, you will need to verify the account again with a code sent to the email, and login to the account.
It will be redirected to a URL as below.
https://login.salesforce.com/?code=aPrxYXyxzkuBzbiNknnlq2OFfWuX.EU66JOpBnNb_.rLpGZu.FdWAZXvDF6PtpoNWUjnrjYX8g%3D%3D
10. Note down the value of the code. In my case, it is “aPrxYXyxzkuBzbiNknnlq2OFfWuX.EU66JOpBnNb_.rLpGZu.FdWAZXvDF6PtpoNWUjnrjYX8g%3D%3D”
11. Now import the following in POSTMAN and obtain the tokens.
curl --location --request POST "https://ap16.salesforce.com/services/oauth2/token?code=aPrxYXyxzkuBzbiNknnlq2OFfWuX.EU66JOpBnNb_.rLpGZu.FdWAZXvDF6PtpoNWUjnrjYX8g%253D%253D&grant_type=authorization_code&client_id=3MVG9G9pzCUSkzZtNiO9KrUineTIaJzO7xLokQLSZ7Xb8mnRgsMC.J6EZNQ9lA.HIxMg7LRmCpxdH.mnU_1au&client_secret=37E2B8478E8C6ADBFB4045466CCB98AA067CE9D8D8A4E3F17D2440B13F046740&redirect_uri=https://login.salesforce.com" \--header "Accept: */*" \--header "Accept-Encoding: gzip, deflate" \--header "Cache-Control: no-cache" \--header "Connection: keep-alive" \--header "Content-Length: 0" \--header "Host: ap15.salesforce.com" \--header "Postman-Token: 7d68f566-7907-443d-9cbc-f5a7205ff1af,7239d98b-8020-47cc-9922-595ef03c676c" \--header "User-Agent: PostmanRuntime/7.19.0" \--header "cache-control: no-cache"
12. Once it is imported, you will have the following in POSTMAN. Replace the following fields with your values.
- code
- client_id
- client_secret
13. Then you will get a reply tokens as above. You can use them to call the Salesforce REST API.
Building the REST API using Integration Studio
In this use-case, we are going to receive the details about the Salesforces themes.
- Open WSO2 Integration Studio and create an ESB Solution Project.
2. Our API would be looking as below(source view).
3. Right-click on the Composite Application Project and click on ‘Export Project Artifacts and Run’. Select ‘Run on Micro Integrator’.
4. Micro Integrator will be started and the carbon application will be deployed. You can further refer to the application deployed through the CLI tool.
Make sure you first export the PATH as below.
$ export PATH=/path/to/mi/cli/directory/bin:$PATH
Then login.
./mi remote login
Provide default credentials admin for both username and password.
In order to view the APIs deployed, execute the following command.
./mi api show
It will display the URL.
Invoke the themecolor operation as below. It can be used Postman to invoke the API.
URL: http://localhost:8290/salesforcerest/themecolor
Resource: POST
Payload.json
{
"accessToken":"AQoAQDvKEfoj9LuPs_P2cne_ws.WH4WNLKaLRNxZp4UHIAKq9JV6Gf5GzZjAWTAzEkDrgJNpvGtLh3yau34DuNQp9MJ.Xgs.",
"apiUrl":"https://ap15.salesforce.com",
"clientId": "",
"refreshToken": "",
"clientSecret": "",
"hostName": "https://login.salesforce.com",
"apiVersion": "v32.0",
"intervalTime" : "100000",
"registryPath": "connectors/SalesforceRest"
}
You will get a response as below.
For further reading, please refer: