Authenticating Salesforce using OAuth 2.0
home
This example shows how to connect to Salesforce using OAuth as the security protocol.
Example
This application employs the Salesforce OAuth connector to enable OAuth authentication before performing the integration process. Salesforce is queried to retrieve all contacts from the Salesforce account. To keep it simple, the Process Records Phase only prints the contacts to the log.
Set Up and Run the Example
- Open the example application in Anypoint Studio. Do not run the application.
- Log in to your Salesforce account. From your account menu (your account is labeled with your name), select Setup.
- In the left navigation bar, under the Build heading, click to expand the Create folder.
- Click Apps. Under Connected Apps click New.
- Fill in all the required fields. Check Enable OAuth Settings. Set Callback URL to http://localhost:8081/oauth2callback. Move Full Access (full) to Selected OAuth scopes. Click Save. Stay on this page.
- In your application in Studio, click the Global Elements tab and add Salesforce (OAuth) connector, if does not already exist.
- Now, paste the Consumer Key and Consumer Secret in the common.properties file under src/main/resources. ( Another option is to Double-click the Salesforce (OAuth) global element to open its Global Element Properties panel. In the Consumer Key and Consumer Secret fields, paste the values from Salesforce App form. Alternatively, configure the global element in the XML Editor. Then click OK to save your changes.)
- In the Package Explorer, right-click the authenticating-salesforce-using-oauth2 project name, then select Run As > Mule Application. Studio runs the application on the embedded server.
- Open your browser and put http://localhost:8081 in the address bar. You are redirected to Salesforce Login page and asked for your credentials. Allow access for this app.
Go back to Anypoint Studio and the console log should contain lines like these:
INFO 2017-10-27 12:49:25,779 [[authenticating-salesforce-using-oauth2].connector.http.mule.default.receiver.02] org.mule.examples.LoggerIterator: contact: {LastModifiedDate=2017-08-25T13:21:00.000Z, Id=0032000001INNfoAAH, LastName=Pickwick, type=Contact} INFO 2017-10-27 12:49:25,779 [[authenticating-salesforce-using-oauth2].connector.http.mule.default.receiver.02] org.mule.examples.LoggerIterator: contact: {LastModifiedDate=2017-08-25T13:21:00.000Z, Id=0032000001INGnuAAH, LastName=GaultThe, type=Contact} INFO 2017-10-27 12:49:25,780 [[authenticating-salesforce-using-oauth2].connector.http.mule.default.receiver.02] org.mule.examples.LoggerIterator: contact: {LastModifiedDate=2017-08-25T13:21:00.000Z, Id=0032000001IOBe4AAH, LastName=Hobbit, type=Contact} INFO 2017-10-27 12:49:25,780 [[authenticating-salesforce-using-oauth2].connector.http.mule.default.receiver.02] org.mule.examples.LoggerIterator: contact: {LastModifiedDate=2017-08-29T15:48:11.000Z, Id=0032000001IOuBtAAL, LastName=Darko, type=Contact} INFO 2017-10-27 12:49:25,780 [[authenticating-salesforce-using-oauth2].connector.http.mule.default.receiver.02] org.mule.examples.LoggerIterator: contact: {LastModifiedDate=2017-09-11T19:19:58.000Z, Id=0032000001IP8uiAAD, LastName=Burke, type=Contact}