+ Start a Discussion
Rocks_SFDCRocks_SFDC 

[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}] Error in REST API

Hi Team,

When we try to send the REST API request from one salesforce instance (Sandbox) to another salesforce instance (Prod). We are receiving the following error:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

HttpRequest req= new HttpRequest(); 
req.setMethod('GET'); 
String username = 'test@salesforce.com'; 
String password = 'test123'; 
Blob headerValue = Blob.valueOf(username + ':' + password); 
String authorizationHeader = 'Basic' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader); 
Http http = new Http(); 
String url = 'https://na1.salesforce.com/services/data/v36.0/tooling/query?q=Select+Id,Name,TableEnumOrId,LastModifiedDate+From+WorkflowRule+Where+LastModifiedDate>2016-01-01'; 
req.setEndpoint(url ); 
HttpResponse res = http.send(req); 
System.debug('AAAAAAAA' +res.getBody());

Could you please anyone check and let us know what modifications required on above code.

Thanks,
Anil


        
_Prasu__Prasu_
I dont think you can directly use the basic authentication. REST API's are available through OAuth 2.0 authentication. You will need to first obtain a access token and then use it in query call. For more information you can refer this link (https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_authentication.htm).