I am calling a REST service written in another salesforce instance using oauth but I am getting the below error in developer console

[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

Class : getting account record from other salesforce org through rest api integration by calling class and its method from other org

public class TestWebService{
    public TestWebService(){
        //You will get "clientId","clientSecret" when creating connected app in salesforce
        String clientId = 'XYZ'; //redacted
        String clientSecret = 'ABC'; //redacted

        //We can also store our username password in custom setting.
        String username='xxx';//salesforce username 
        String password='xxxx';//salesforce password+securitytoken
        // Generating the Access Token
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');// this is the OAuth endpoint where this request will be hit

        Http http = new Http();
        HTTPResponse res = http.send(req);
        String str = res.getBody();
        wrapObj = (Wrapper)Json.deserialize(str,Wrapper.class);
        accessToken = wrapObj.access_token;
        instanceUrl = wrapObj.instance_url;

    // Retrieve the Contacts from the otehr org.
    public void methodGet(){
        HttpRequest req = new HttpRequest();

        req.setHeader('Authorization', 'Bearer'+wrapObj.access_token);
        Http http = new Http();
        HTTPResponse res = http.send(req);

        System.debug('***Response***** ' + res.getBody());
        //---------------------Here ------------------
        /*JSONParser parser = JSON.createParser(res.getBody());
        }while(parser.hasCurrentToken() && !'records'.equals(parser.getCurrentName()));
        List<account> acc = (List<Account>) parser.readValueAs(List<Account>.class);*/
    public Contact con {get;set;}
    public String s1 {get;set;}
    public String accessToken;
    public String instanceUrl;
    public Wrapper wrapObj{get;set;}
    // Wrapper Class to Store the value from the Jason.
    public class Wrapper{
        String id;
        String instance_url;
        String access_token;
        String issued_at;
        String signature;

Invoking in developer console

TestWebService tw = new TestWebService();

Not sure why I am getting the error.

Please let me know where I am making the mistake.

  • I think you shouldn't be sharing some of this data like client secret. Anyway I think you just missed the space after Bearer since it should be Bearer <token>. Try changing the string prefix to 'Bearer ' + ... – Adrian Larson Feb 18 '17 at 15:45
  • I have done this in my developer org.I will keep this in mind.Added space but still no luck even when I am directly trying to access this url lightningwebsite-dev-ed.my.salesforce.com/services/apexrest/… I am getting the same error however I am getting the response through workbench. – Debarun Sengupta Feb 18 '17 at 15:46
  • 1
    I think it's pretty apparent that your problem is limited to authentication. You have this one line in your code sample that causes a little concern... //We can also store our username password in custom setting. Rather than using a Custom Setting, you should look into a Named Credentials. The Named Credentials feature was designed for your EXACT use case.... accessing an external service from Salesforce using Oauth. There's also lots of examples to be found. – John Thompson Feb 18 '17 at 20:34
  • Hey there was an authentication problem in my code.I fixed it.The username and the password which I was passing was of of the source org rather it should be of the destination org. – Debarun Sengupta Feb 20 '17 at 6:43
  • @JohnThompson - thanks for the tip -- I had forgotten about these – cropredy Feb 21 '17 at 5:26

Your Answer

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

Browse other questions tagged or ask your own question.