21

I am trying to integrate one Salesforce org to other org by using Oauth setup, I wanted to use use consumer key and consumer secret key in my REST class to connect with the org, after connection part completion, I want to access Get(), Put(), Post(), etc. Including the ability to set Oauth and Authorization headers needed for Salesforce Rest API calls.

Actually I'm new in REST implementation so can you please explain me by using code samples.

25

One of the ways to Integrate two Salesforce Instances will be using oauth for session management and using REST API

Lets Assume you have SalesforceA as Source ORG and SalesforceB as Destination ORG .Now lets assume flow of data is from SalesforceA to SalesforceB Instance

1)For oauth 2.0 in Destination org(SalesforceB) create a Connected App .To help you configure the Connected App in Destination below screenshot should assist.Callback Url may not be of any significant as we are using User-Name Password flow model.You will obtain client secret,consumer key in this process and one should store in Source org custom object or custom setting

enter image description here

2)In your source org create a Remote site settings with url as the URL of the Destination Instance

3)Create a Integration User in the Destination org whose credentials you will use to call API from Salesforce Source ORG

4)Use the username,password and security token(Also i would prefer setting password never expires here for integration User) and also client secret and consumer key in a custom object or custom setting (Protected) in Source org.

The below code is sample code to get authenticated and how to use access token to further make any API call

Settings__c ts=settings;//Here write a sample query or fetch from custom settings the consumer ,client secret and username and password of destination org

String clientId = ts.ConsumerKey__c;
String clientSecret = ts.Client_Secret__c;
String username=ts.Username__c;
String password=ts.Password__c+ts.SecurityToken__c;


String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint(ts.URL__c+'/services/oauth2/token');//Note if my domain is set up use the proper domain name else use login.salesforce.com for prod or developer or test.salesforce.com for sandbox instance

HttpResponse res = h.send(req);

OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
RequestWrapper reqst=new RequestWrapper();

if(objAuthenticationInfo.access_token!=null){



  Http h1 = new Http();
  HttpRequest req1 = new HttpRequest();
  req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
  req1.setHeader('Content-Type','application/json');
  req1.setHeader('accept','application/json');

  req1.setBody(jsonstr);//Send JSON body
  req1.setMethod('POST');
  req1.setEndpoint(ts.URL__c+URL);//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
  HttpResponse res1 = h1.send(req1);
  system.debug('RESPONSE_BODY'+res1 .getbody());

To deserialize the initial response here is wrapper class the above code uses

/*To get aouthentication detail Wrapper*/
public class OAuth2{
 public String id{get;set;}
 public String issued_at{get;set;}
 public String instance_url{get;set;}
 public String signature{get;set;}
 public String access_token{get;set;}    
}
  • How can i implement the same without using users credentials. – sneha Jan 24 '14 at 11:45
  • 1
    Well you cant implement without user credentials .You need an integration user in any oauth flow that you use sneha – Mohith Shrivastava Jan 24 '14 at 11:45
  • 1
    @sneha JSON will be the data that you are sending to other instance and that code is raw version and it will just give a brief idea of how to proceed – Mohith Shrivastava Jan 28 '14 at 7:01
  • 1
    can any one please tell me in this implementation how can i use get method in OAuth2.0 webserverflow for querying some data from one org. – sneha Jan 28 '14 at 13:57
  • 1
    I would update the answer to make sure that the strings are URL encoded. I kept getting a bad request response from the server because I did not have my pw encoded. - EncodingUtil.urlEncode(password, 'UTF-8') – jmdohn Dec 28 '16 at 16:20
2

A method for doing this without storing credentials is described here: https://developer.salesforce.com/blogs/isv/2015/04/integrating-multi-orgs-using-oauth.html

In brief: IN HUB ORG 1. Generate a X509 Certificate file and Private Key 2. Create a Connected App in the Salesforce Hub Org 3. Click the Use digital signatures checkbox, click the Choose File button and select the Certificate file that was created above (e.g. mycert.cer) 4. Set OAuth policies 5. For the Require Users to Log in radio button, select Refresh Token is valid until revoked. 6. Add Approved Profile to the Connected App IN SPOKE ORG: 7. Update Remote Site Settings 8. Create Apex Code (sample here)

  • Please add a rough gist of the linked content here if it answers the question. Links may go dead over time, rendering link-only answers completely useless. – Adrian Larson Feb 16 '18 at 21:42

protected by Adrian Larson Mar 16 '18 at 19:07

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.