You need to sign in to do that
Don't have an account?
SK9
Salesforce to Salesforce REST Callout
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"}]
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"}]
Please follow the comments made by "Ken Koellner 7" with code snippets in the below link and you should be able to achieve what you are looking for.
https://developer.salesforce.com/forums/?id=906F0000000AgInIAK
Hope this helps.Mark this as best answer if it works, so others can benefit too.
Regards,
Santosh.
Just try this sample code, it will help you In above your code, content type have missed,
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
Kindly add and execute!!
Thanks,
Thank you for your reply.
I added the header but it is still giving me - {"error":"invalid_grant","error_description":"authentication failure"}
Thank you for your quick reply. I went through the link provided and followed the steps in Postman, but I got the attached error .
Please kindly check password=yourpasswordANDsecuritytoken.
Thanks,
Hi @SK9,
Please follow this link and you should be able to solve your issue.
https://blog.webnersolutions.com/salesforce-mobile-error-oauth_approval_error_generic
Hope this helps!
Regards,
Santosh.