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 

REST API 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
Carl PierreCarl Pierre
Hi,


@Deepak Gulian

what sould I use as securitytoken? since I'm not able to get acces token.

array(
    'grant_type' => 'password',
    'client_id' => CLIENT_ID,
    'client_secret' => CLIENT_SECRET,
    'username' => 'kevin.xxx@cvent.com.devpro3',
    'pasword' => $code,
);
Deepak GulianDeepak Gulian

You must append the user’s security token to their password A security token is an automatically-generated key from Salesforce. For example, if a user's password is mypassword, and their security token is XXXXXXXXXX, then the value provided for this parmeter must be mypasswordXXXXXXXXXX.

https://developer.salesforce.com/docs/atlas.en-us.200.0.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm

Carl PierreCarl Pierre

Thanks for your help but I tried this using my own credentials, I'm still getting authentication failure.  :


Body='grant_type=password&client_id=3MVG9Y6d_Btp4xp6i7amMyRkpuMUTRLqeqNYTr036XK7YoDYXlaWbZgJ_kLWXSJJQvJX50olzBSiy6jZOh.8r&client_secret=1595478626856505123&username=username&password=passwordsecuritytoken';


Do you know if there a way to have more details about this error? 

Thanks

 

 

Deepak GulianDeepak Gulian
Can you please share your code?
Deepak GulianDeepak Gulian
This will work for you as you able to get an authorization code

Body ='grant_type=authorization_code&client_id=clientId&client_secret=clientSecret&redirect_uri=redirecturl&code=authorizationcode';

Replace:-
redirecturl = Redirect URL
clientId = Client ID
clientSecret = Client Secret
authorizationcode = Authorzation Code you recieved
Carl PierreCarl Pierre
​$params = array(
    'code' => $code,
    'grant_type' => 'authorization_code',
    'client_id' => CLIENT_ID,
    'client_secret' => CLIENT_SECRET,
    'redirect_uri' => REDIRECT_URI
);

//var_dump($params);

$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); // to be removed in PROD
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

$json_response = curl_exec($curl);


This is my PHP code

 

This is the error message

 

Error message

Thanks for you support

Jason TisseraJason Tissera

Hello all, I have a packaged connected app, I am able to authenticate to salesforce through it successfully when I login to one client (using production salesforce), but when one of my clients tries to authenticate to it, they are getting the error you listed above. 

One difference between the client that works and the client that doesn't is that the client that doesn't is using a sandbox environment.

This is the error I receive from the client that doesn't work. Below it I have included the options that I send to salesforce:

Response from Salesforce:

error: { error: 'invalid_grant', error_description: 'authentication failure' }
 

Request sent to Salesforce:

options: { method: 'POST', uri: 'https://login.salesforce.com/services/oauth2/token',
qs: {

grant_type: 'authorization_code', code: 'aPrxvJDqQzo3F9Zv2.JZUE6OKGkNgIyEElf.lmuggQAy6tg3C_xzrSzm9jDfuZqY_wU3KpeCBA==',

client_id: '****hidden*****',

client_secret: '****hidden*****',

redirect_uri: 'https://******/oauthcallback' },

headers: { Host: 'login.salesforce.com', 'content-type': 'application/x-www-form-urlencoded' }
 

Appreciate any guidance from the comunity.