0

I'm trying to get an Auth Token (using PHP) and having trouble getting back the authorization code so I can make the next call and get the Auth Token. I set up a connected APP in the console and have the correct Client ID, Secret and callback URI in the initial API call to: https://na8.salesforce.com/services/oauth2/token - but the cURL call is returning:

{"error":"invalid_grant","error_description":"invalid authorization code"}.

I was under the impression that once the first call was made, the authorization code would be included as a $_GET variable in the callback URL, but it doesn't seem to be happening (I'm logging anything that hits that page with a $_REQUEST variable).

Here is the format of the post variables I'm sending on the first call. The constants are properly defined with the correct values:

$arrArgs = array(
    'client_id' => SF_APP_CONSUMER_KEY,
    'client_secret' => SF_APP_SECRET,
    'redirect_uri' => SF_APP_URI,
    'grant_type' => 'authorization_code',
    "response_type" => 'code'
);

Any help would be very much appreciated.

1

You need to use grant_type="password" as authentication flow and your request will form as below

$arrArgs = array(
    'client_id' => SF_APP_CONSUMER_KEY,
    'client_secret' => SF_APP_SECRET,
    'client_secret' => SF_APP_SECRET,
    'username' => SF_USERNAME,
    'password' => SF_PASSWORD+SECURITYTOKEN,
    'redirect_uri' => SF_APP_URI,
    'grant_type' => 'password'
);

I would recommend to use PHP SDK which will ease your work. please check following example for better understanding of different method available using SDK

https://developer.salesforce.com/page/PHP_Toolkit_20.0_Samples

  • Thank you for the response. I didn't look deeply at the SDK Link and toolkit samples, but it seems like that is using SOAP and not the REST API (I could be wrong there). What I need to actually do once I can get the auth_code is so minimal, I think using the REST API is the way to go. Am I wrong in this thinking? – Kirk B Dec 29 '15 at 18:15
  • if you will check SDK you will understand that it will fulfil your all requirement whether it is creating new record, editiing, delete etc. otherwise you can use above code to get Auth TOKEN – Himanshu Dec 29 '15 at 18:18
  • Himanshu, thank you for the help. Adding in those post values did the trick. – Kirk B Dec 29 '15 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.