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
Carl PierreCarl Pierre 

Authentication failure

Hi, get this message following the steps provided here : https://developer.salesforce.com/page/Getting_Started_with_the_Force.com_REST_API

So after getting the autorization code I trying to the access token and I have this message:

Error: call to token URL https://login.salesforce.com/services/oauth2/token failed with status 400, response {"error":"invalid_grant","error_description":"authentication failure"}, curl_error , curl_errno 0

I Have https url with a certificate.

I'm not able to find a support team or more info aobut the error



 

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Carl,

You can try this -
String uri          = 'https://login.salesforce.com/services/oauth2/token';
String clientId     = EncodingUtil.urlEncode('YOUR CLIENT ID','UTF-8');
String clientSecret = EncodingUtil.urlEncode('YOUR CLIENT SECRET','UTF-8');
String username     = EncodingUtil.urlEncode('USER NAME','UTF-8');
String password     = EncodingUtil.urlEncode('PASSWOED','UTF-8');
String body =   'grant_type=password&client_id=' + clientId + '&client_secret=' + clientSecret +'&username=' + username + '&password=' + password; 
HttpRequest req = new HttpRequest();
req.setEndpoint(uri);
req.setMethod('POST');
req.setbody(body);
Http http = new Http();
HTTPResponse response = http.send(req);
system.debug('Response is ::: '+response.getbody());
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(response.getbody());
System.debug(m.get('access_token'));

// Creating a account Record
Http h = new Http();
HttpRequest requ = new HttpRequest();
requ.setEndpoint(m.get('instance_url')+'/services/data/v34.0/sobjects/Account');
requ.setMethod('POST');
requ.setHeader('Authorization','Bearer '+String.valueof(m.get('access_token'))); 
requ.setbody('{"Name" : "Mohit Logistics and Transport"}');
requ.setHeader('Content-Type', 'application/json');  
HttpResponse res = h.send(requ);
system.debug(res.getbody());
You can run this code on the developer console.

Thanks,
Sumit Kumar Singh
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hi Carl,
 If you using cURL then -
Replace the parameters like clinent_id, client_secret with your own.
curl -v -k https://login.salesforce.com/services/oauth2/token -d "grant_type=password&client_id=RiVk4&client_secret=66&username=abc@xyz.com&password=Jxyz" -H "X-PrettyPrint:1"+
This will return the JSON response -  copy the access token from the JSON response -
To get all the sObject Information  -
curl -H "Authorization : Bearer Access_Token_From_JSON_Respnse" https://ap2.salesforce.com//services/data/v20.0/sobjects/ -H "X-PreetyPrint:1"

Hope, it will help you.

Thanks,
Sumit Kumar Singh