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
Daniel Ramirez PerezDaniel 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.


I want to replicate this in Salesforce
VinayVinay (Salesforce Developers) 
Hi Daniel,

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
Daniel Ramirez PerezDaniel Ramirez Perez
Hi.
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.
Abhishek MahajanAbhishek Mahajan

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.

prithviraj patil 3prithviraj patil 3

Hi,

you can try the below code 

Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
String token = '';
String refreshtoken = '';
if(response.getStatusCode() == 200){
 token = (String)results.get('access_token');
 refreshtoken = (String)results.get('refresh_token');
}

Please mark it as Best Answer as it will help others looking for a similar solution.

Thanks,
Prithvi.

Divyansh Sharma 5Divyansh Sharma 5
Line: 11, Column: 1
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...!
Divyansh Sharma 5Divyansh Sharma 5
Also, @Daniel Ramerez Perez, while using your code, it does give the token.

Please see attached screenshot.

User-added image

Could anyone please advice?