Skip to main content
Sharda Kumari (airbnb) a posé une question dans #Integration
Hi, 

I am struggling with sending a REST Callout from one Salesforce org to another. I am not able to get the access token. Here is my code snippet - 

** Callback url in the connected app on target org - https://<domainName>.my.salesforce.com/services/oauth2/token

String clientId = 'consumer key  from the connected app in the target org';

String clientSecret = 'consumer secret from the connected app in the target org';

String username = 'my username in the target org';

String password = 'my password in the target org';

HttpRequest req = new HttpRequest();

Http h = new Http();

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

req.setMethod('POST'); 

req.setEndpoint('https://test.salesforce.com/services/oauth2/token'); 

HttpResponse res = h.send(req); 

System.debug(res.getBody()); 

deserializeResponse der = (deserializeResponse) JSON.deserialize(res.getBody(), deserializeResponse.class);

String accessToken = der.access_token;

**** I get an error Here - {"error":"invalid_grant","error_description":"authentication failure"}

HttpRequest req2 = new HttpRequest(); 

req2.setEndpoint('https://<domainName>.my.salesforce.com/services/apexrest/<RestResourceClassName>/<HttpPost method name>'); 

req2.setHeader('Authorization','Bearer ' + accessToken); 

req2.setHeader('Content-Type', 'application/json');

req2.setMethod('POST'); 

req2.setBody( body ); 

Http h2 = new Http(); 

HttpResponse res2 = h2.send(req2);

**** Second callout error is as a result of null access token i believe  - [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

 
6 réponses
0/9000