0

We are trying to migrate data from one sales force org to other using REST web service. Created REST service in one org trying use that service from other org but getting error "System.HttpResponse[Status=Unauthorized, StatusCode=401]".

Http Callout :

String username = correct usernme;
String password = Correct password;

Blob headerValue = Blob.valueOf(username + ':' + password);
     String authorizationHeader = 'Basic' +
     EncodingUtil.base64Encode(headerValue);

HttpRequest req = new HttpRequest();
req.setEndpoint('https://ap4.salesforce.com/services/apexrest/Accounts/');
//req.setHeader('Authorization', authorizationHeader);
req.setMethod('GET');
//req.setHeader('content-type', 'application/json');  
req.setTimeout(120000);
//req.setBody(jsonBody);                
Http htp = new Http();
HttpResponse resp = htp.send(req); 
System.debug('resp:'+resp);

HOw should i call one sales force rest web service from another salesforce org?

1

This can easily be achieved in a few steps:

  1. Create a Connected App on the Remote org.
  2. Create an Auth. Provider in your local org, using Salesforce Authentication.

    • In your Auth. Provider you will need to add elements from your Connected App.

      • Client Key
      • Client Secret
    • Save your Auth. Provider and Copy the "Callback Url" at the bottom of the view.

    • Paste the Callback Url to the Connected App in the remote org.
  3. Create a Named Credential in your local org.

    • URL: Url of the remote org
    • Identity Type: Named Principle
    • Authentication Protocol: OAuth 2.0
    • Authentication Provider: Auth. Provider created above
    • Scope: refresh_token full
    • Start Authentication Flow on Save: TRUE

NOTE : There will be a few minutes delay between availability of your end point after you save the Callback Url to the Connected app.

  1. Authenticate your Named Credential

Once you have authenticated your Named credential, you can use the named credential to make your callouts to the remote organization. An example of the callout using the named credential would look like:

HttpRequest feedRequest = new HttpRequest();
feedRequest.setEndpoint('callout:<named_credential_name_here>/services/data/v32.0');
feedRequest.setMethod('GET');
Http http = new Http();
HTTPResponse feedResponse = http.send(feedRequest);
System.debug(feedResponse.getBody());

What you do with your response you can handle in your own way. This should get you on your way. Good luck!

Links for additional Help:

Blog: https://www.jitendrazaa.com/blog/salesforce/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code/

SF Docs:

Named Credentials

Auth. Provider

Merge Fields In Callouts With Named Credentials

0

You cannot use Basic authentication to connect another salesforce org.

There are two ways you can connect to salesforce org either create a remote application and get the client and secret key from the target system, or try to use soap login methods in the rest api.

Please find the below sample snippet.

HttpRequest request = new HttpRequest();
        request.setEndpoint('https://www.salesforce.com/services/Soap/u/36.0');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' +uname + '</username><password>' + password+'</password></login></Body></Envelope>');
        httpresponse res = new http().send(request);

Please send the valid user name and password+Token, you will recieve session id and you can make another request using the session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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