Salesforce Integration: Authorize one salesforce org from another using Rest API

This article will guide you through the OAuth 2.0 authorization process, demonstrating the Salesforce integration of two orgs. Stay tuned for upcoming articles where we explore creating custom APIs to transfer data between Salesforce orgs.

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 :

  1. username = Salesforce Org’s Username
  2. password = Salesforce Org’s password + security Token
  3. client_id = Consumer Key (Find in Connected App – Org1)
  4. client_secret = Consumer Secret (Find in Connected App – Org1)
  5. 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 = ''; 
       //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.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        // 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:outputPanel id="accessTokenSection">
                <apex:pageBlock title="Access Token">
                    <p>Access Token: {!accessToken}</p>


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.

Leave a Reply

Your email address will not be published. Required fields are marked *