You need to sign in to do that
Don't have an account?
Nikhil Shrigod 25
Unable to retrive the access token while doing a callout for Apex REST API which is present in another salesforce org.
public class SendAccountUsingRESTAPI {
private final String clientId = 'XXXX';
private final String clientSecret = 'XXXX';
private final String username = 'XXXX';
private final String password = 'XXXX';
public class deserializeResponse
{
public String id;
public String access_token;
}
public String ReturnAccessToken (SendAccountUsingRESTAPI acount)
{
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://ap4.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
system.debug('@@@@access_token@@ = '+resp1 );
return resp1.access_token;
}
}
In this class I am trying to get the access token with the help of ReturnAccessToken() method. But due to some reason it is returning null. I am not getting the access token. All the details I get is Status code = 400 and Status = Bad request. Can anyboody please give a solution to this problem, because I've got stuck here for a long time.
private final String clientId = 'XXXX';
private final String clientSecret = 'XXXX';
private final String username = 'XXXX';
private final String password = 'XXXX';
public class deserializeResponse
{
public String id;
public String access_token;
}
public String ReturnAccessToken (SendAccountUsingRESTAPI acount)
{
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://ap4.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
system.debug('@@@@access_token@@ = '+resp1 );
return resp1.access_token;
}
}
In this class I am trying to get the access token with the help of ReturnAccessToken() method. But due to some reason it is returning null. I am not getting the access token. All the details I get is Status code = 400 and Status = Bad request. Can anyboody please give a solution to this problem, because I've got stuck here for a long time.
You need to add a security token along with the password.
More Info:
https://www.youtube.com/watch?v=7PaDtgPLH90
After watching this video still if you have an issue please let me know I will help you.
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Salesforce Freelance Consultant/Developer/Administrator/Trainer
@For Salesforce Project Support: varaprasad4sfdc@gmail.com
Salesforce latest interview questions and training videos :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Thanks
Varaprasad
req.setheader('content-type' : 'application/x-www-form-urlencoded');
and I also saw your video but still I am getting token = null.
Even in postman it is giving :
{
"error": "invalid_grant",
"error_description": "authentication failure"
}