function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SK9SK9 

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"}]
 
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Hi SK9,

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.
Priyananth RPriyananth R
Hi,

Just try this sample code, it will help you
HttpRequest req = new HttpRequest();
Http h = new Http();
req.setMethod('POST'); 
req.setEndpoint('https://login.salesforce.com/services/oauth2/token?grant_type=password& client_id= 3MVG9iTxZANhwsdsdsdsdspr0Lu3QNRNKk4c2FejzTys5Mlp43UeSHBuhWWgRjEUyV6xE7N0GostjR3sRat & client_secret=21961212323233121943 & username=priyananth@ibirds.com & password=myPWDAndSecurityToken'); 
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
HttpResponse res = h.send(req); 
System.debug(res.getBody());
In above your code, content type have missed, 
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
Kindly add and execute!!

Thanks,
SK9SK9
Hi, Priyanath, 
Thank you for your reply. 
I added the header but it is still giving me - {"error":"invalid_grant","error_description":"authentication failure"}
SK9SK9
Hi @Santosh Reddy,
Thank you for your quick reply. I went through the link provided and followed the steps in Postman, but I got the attached error . User-added image
Priyananth RPriyananth R
Hi,

Please kindly check password=yourpasswordANDsecuritytoken.

Thanks,
 
Santosh Reddy MaddhuriSantosh Reddy Maddhuri

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.