There might be a situation where we need to integrate two Salesforce orgs. In this article, we’ll look at how to authorize one Salesforce org to another using the OAUTH 2.0 standard protocol, facilitated by the Connected app (the framework that resides inside Salesforce) that we saw in the previous article ( How to configure connected app ).
In order to begin, we must have two developer edition Salesforce orgs. In the first org, we will establish a connected app, from which we can obtain the consumer key and consumer secret:
To learn how to create a connected app, also check out the blog: Connected App
We need to have the following information from the first org :
- username = Salesforce Org’s Username
- password = Salesforce Org’s password + security Token
- client_id = Consumer Key (Find in Connected App – Org1)
- client_secret = Consumer Secret (Find in Connected App – Org1)
- grant_type = password (password as text, not your actual password)
Now Log in to the second org. Launch the developer console, create a new Apex class, and use the code below to create an HTTP request to hit the Salesforce Authorization end point for production. This code utilises all of the information that we have obtained from the first org.
//Use this Apex class for getting the Access Token from Org1 global class IntegrationClass { public static string accessToken{get; set;} public static void getAccessToken() { String clientId = 'CLIENT_ID_Org1'; String clientSecret = 'CLIENT_SECRET_Org2'; String username = 'USERNAME_Org1'; String password = 'PASSWORD_Org1'; String securityToken = 'SECURITY_TOKEN_Org1'; // Define the login URL String loginUrl = 'https://login.salesforce.com/services/oauth2/token'; //if the sandbox URL(test in place of login) if it's a sandbox org // Construct the request body String requestBody = 'grant_type=password' + '&client_id=' + clientId + '&client_secret=' + clientSecret + '&username=' + username + '&password=' + EncodingUtil.urlEncode(password + securityToken, 'UTF-8'); // Encoding the password // Create the HTTP request HttpRequest request = new HttpRequest(); request.setEndpoint(loginUrl); request.setMethod('POST'); request.setHeader('Content-Type', 'application/x-www-form-urlencoded'); request.setBody(requestBody); // Send the request Http http = new Http(); HttpResponse response = http.send(request); // Parse the response if (response.getStatusCode() == 200) { Map<String, Object> authResponse = (Map<String, Object>)JSON.deserializeUntyped(response.getBody()); System.debug('authResponse: ' + authResponse); accessToken = (String)authResponse.get('access_token'); instanceUrl = (String)authResponse.get('instance_url'); System.debug('Access Token: ' + accessToken); } else { System.debug('Failed to get Access Token. Status code: ' + response.getStatusCode() +' ,Body: ' + response.getBody()); } } }
Use the Visualforce page code below to display the access token.
<!--Visualforce page to display Access Token --> <apex:page controller="IntegrationClass"> <apex:form > <apex:pageBlock title="Access Token Demo"> <apex:pageMessages /> <apex:pageBlockSection > <apex:commandButton action="{!getAccessToken}" value="Get Access Token" rerender="accessTokenSection" styleClass="btn colorful-btn"/> </apex:pageBlockSection> <apex:outputPanel id="accessTokenSection"> <apex:pageBlock title="Access Token"> <p>Access Token: {!accessToken}</p> </apex:pageBlock> </apex:outputPanel> </apex:pageBlock> </apex:form> </apex:page>
Now, preview the visualforce page and click the button “Get Access Token” to fetch the access token fron the org1. We have successfully integrated two salesforce orgs.
In upcoming articles, we will see how we can create custom APIs to transfer data from one Salesforce org to another.