You need to sign in to do that
Don't have an account?
Daniel Ramirez Perez
get access token from external system
Hi guys. I'm trying to make a GET method from apex to generate an Access Token. I have to pass the access token to a URL in order to retrieve a JSON response. I don't know how to pass the client Id, client secret, endpoint, scope and grant type in apex.
Then I will pass the URL and access token to get the JSON I mentioned.
Please can anyone help me with the code or how to set the Named Credentials. I've been looking for an example but all of them point to the Salesforce connected app.
I'm attaching a capture of the same in Postman. I tryed first in postman and it works.
Thanks in advance.
Then I will pass the URL and access token to get the JSON I mentioned.
Please can anyone help me with the code or how to set the Named Credentials. I've been looking for an example but all of them point to the Salesforce connected app.
I'm attaching a capture of the same in Postman. I tryed first in postman and it works.
Thanks in advance.
Check below links which has examples of rest API integration.
https://www.jitendrazaa.com/blog/salesforce/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code/
https://www.gosquared.com/blog/salesforce-rest-api-integration
https://apexcoder.com/2017/01/26/how-to-call-rest-service-using-oauth-rest-api/
https://jayakrishnasfdc.wordpress.com/2018/09/09/apex-rest-web-services-example/
Hope above information was helpful.
Please mark as Best Answer so that it can help others in the future.
Thanks,
Vinay Kumar
Thanks for your help. But, again, the links point to get data from Salesforce, I need to get an access token from an external site and then use the token with another url to get a JSON response that I have to deserialize. What I need is the token, I just found an example on doing it:
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setEndpoint('https://ap5.salesforce.com/services/oauth2/token');
String CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
String CLIENT_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXX';
req.setBody('grant_type=client_credentials' + '&client_id='+CLIENT_ID + '&client_secret='+CLIENT_SECRET + '&scope=USER');
Http http = new Http();
HTTPResponse response = http.send(req);
System.debug('Body ' + response.getBody());
System.debug('Status ' + response.getStatus());
System.debug('Status code ' + response.getStatusCode());
I don't need to give a password and username, just client id and client secret. I works.
In the body I get an access_token and a refresh_token.
My question now is how can I assign the access and refresh token to variables.
Regards.
Hi Daniel,
Did you get waht you are looking for? Because I am facing the ame issue, I got the token and now I need to use that to make post the data in other system.
Hi,
you can try the below code
Please mark it as Best Answer as it will help others looking for a similar solution.
Thanks,
Prithvi.
System.JSONException: Unexpected character ('R' (code 82)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]
I am using the above code, but getting the error. Please help...!
Please see attached screenshot.
Could anyone please advice?