+ Start a Discussion
Anudeep BAnudeep B 

REST API Invalid session or session Expire Error

I am trying to Integrate from one SalesForce to another SalesForce, say SF1(Provider) and SF2(Consumer). I created Connected app and having Consumer key, Consumer Secret and callback URL.

To consume in SF2 i have write this below code.


global class RestTwoStepCall
{
  public string result{set;get;}
  
  public void show()
  {

    Http p = new Http();
    HttpRequest request = new HttpRequest();
    request.setMethod('POST');
    request.setEndPoint('https://login.salesforce.com/services/oauth2/token');
    request.setBody('grant_type=password&client_id=Consumer key(Given) &client_secret=Consumer Secret(Given) &username=UserName(Given)&password=PasswordwithSecurityToken');
    HttpResponse response = p.send(request);
    String resp = response.getBody();
    fromJSON js = (fromJSON)JSON.deserialize(resp,fromJSON.class);
    result = js.access_token;  
    


    HttpRequest req1 = new HttpRequest();
    req1.setMethod('GET');
    req1.setEndPoint('https://ap2.salesforce.com/services/apexrest/Deepus?name=Anudeep&Dept=IT&job=SFDCDeveloper');
    req1.setHeader('Authorization','OAuth '+result);
    HttpResponse res = p.send(req1);
    result = res.getBody();   
    
  }

     public class fromJSON
     {
        public String id;   
        public String issued_at;    
        public String token_type;   
        public String instance_url; 
        public String signature;    
        public String access_token;
        
      }

}



by executing this i got this Error on VF page.. 
[{"message":"Session expired or nvalid","errorCode":"INVALID_SESSION_ID"}]

I have enable api on profile, and did reset security token. where i went wrong.? 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Few days ago, I also accomplished the same thing in my dev org. I have shared my code with you. Can you please refer it?
public class SampleIntegration
{
    public static void integration()
    {
        HttpRequest obj = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        String username = //your username
        String pwd = //your password
        String reqBody = 'grant_type=password&client_id=3MVG9ZL0ppGP5UrDSUSAvgS2A4A1na8ggYshVrJAdM_wuHqvf0VYg4pCB9alTxKKqwiQIQRtZgMfXCu9kO0v0&client_secret=661274095057800454&username='+username+'&password='+pwd;
        obj.setMethod('POST');
        obj.setEndPoint('https://login.salesforce.com/services/oauth2/token');
        obj.setBody(reqBody);
        res = http.send(obj);
        OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
        
        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.setMethod('DELETE');
        String strName = 'Test Pankaj 3';
        req1.setEndpoint('https://ap2.salesforce.com/services/apexrest/createAccount?cName='+EncodingUtil.urlEncode(strName, 'UTF-8'));//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
        system.debug('======req1========'+req1);
        HttpResponse res1 = h1.send(req1);
        system.debug('==========res1============'+res1.getBody());
    }
}

This is the response class for Oauth2:
 
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;}    
}