2

I'm using this to get a starting point token:

session_start();

require '../config.inc.php';

$token_url = "https://test.salesforce.com/services/oauth2/token";
$params    = [
    "grant_type"     => "password",
    "client_id"      => SF_CONSUMERKEY,
    "client_secret" => SF_CONSUMERSECRET,
    "username"       => SF_USERNAME,
    "password"       => SF_PASSWORD . SF_SECURITY
];

//print_r($params);

$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

$json_response = curl_exec($curl);
$status        = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status != 200) {
    die("Error: call to token URL $token_url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}

curl_close($curl);

$response     = json_decode($json_response, true);
$access_token = $response['access_token'];
$instance_url = $response['instance_url'];

echo "Token = " . $access_token;
echo "<br>";
echo "Instance URL = " . $instance_url;

$_SESSION['access_token'] = $access_token;
$_SESSION['instance_url'] = $instance_url;

[source] https://www.veonconsulting.com/salesforce-integration-using-php/

This is my config.php

define("SF_USERNAME", "[email protected]");
define("SF_PASSWORD", "5jZXXXXXXXXXXXXXLfh");
define("SF_SECURITY", "IfXXXXXXXXXXXXXXtPIB");
define("SF_CONSUMERKEY", "XXXXXXXXXXXXXXXXXXXX.XXXXXXXXXXX_XXXXXXXXXX");
define("SF_CONSUMERSECRET", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

Checked all steps in this thread https://stackoverflow.com/questions/12794302/salesforce-authentication-failing

I have tried a total 5 packages on github to test and this one from veon mentioned on top seems the newest, and should be easy.

That said, still getting {"error":"invalid_client","error_description":"invalid client credentials"} after we checked everything on at least 10-20 support threads and forums.

Tried, checked, doublechecked and retried the following:

  • Changed password (to only include aZ09) and updated security token, which helped from getting wrong URL to the next step, a wrong credential
  • Whitelisted IPs according to the post above
  • Set IP Relaxation to Relax IP restrictions
  • Refresh Token Policy: Immediately expire refresh token (only choice)
  • Set Permitted Users All users may self-authorize

Still this error. It's getting frustrating since I am stuck at the door knocking to get in and play :-)

Is there any way we can see more than the given information? I assume it should not be this hard as I am making it to be to get a simple connection to the App and get a token in return.

I'm working in a dev account, like https://company-dev-ed.lightning.force.com/

EDIT:

I made some progress with the help of the VEON script owner.

I tried it again NOT using the security code with password (PASSWORD + SECURITY) and using login.sales... instead of test.sales..... and then it works.

Does the DEV account/area not have a test.sales.... URL? Does it use login.salesforce.com by default because it is a dev account anyway?

2
  • 1
    I don't know PHP, but the ampersand before client_secret appears to be spurious
    – David Reed
    Commented Feb 15, 2021 at 2:14
  • You're right that shouldn't be there (changed the string into an array earlier), but that is not it. Same issue when I removed it.
    – Kim K.
    Commented Feb 15, 2021 at 10:03

1 Answer 1

2

Ok, it's fixed and here's the answer if anybody stumbles across it looking for the same. Key issues were:

  1. The PASSWORD must be JUST the PW, do NOT add the security key to it
  2. Apparently the DEVELOPER account does not use test, but the regular login.salesforce... URL.
session_start();

require '../config.inc.php';

$token_url = "https://login.salesforce.com/services/oauth2/token";
$params    = [
    "grant_type"     => "password",
    "client_id"      => SF_CONSUMERKEY,
    "client_secret"  => SF_CONSUMERSECRET,
    "username"       => SF_USERNAME,
    "password"       => SF_PASSWORD
];

//print_r($params);

$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

$json_response = curl_exec($curl);
$status        = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status != 200) {
    die("Error: call to token URL $token_url failed with status $status, 
response $json_response, curl_error " . curl_error($curl) .
", curl_errno " . curl_errno($curl));
}

curl_close($curl);

$response     = json_decode($json_response, true);
$access_token = $response['access_token'];
$instance_url = $response['instance_url'];

echo "Token = " . $access_token;
echo "<br>";
echo "Instance URL = " . $instance_url;

$_SESSION['access_token'] = $access_token;
$_SESSION['instance_url'] = $instance_url;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .