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
אביב קסוטואביב קסוטו 

"INVALID_SESSION_ID" error after using "access token" REST API PHP

I am trying to retrieve data from salesforce using the REST api and CURL in PHP.

I perform the authentication request and recieve an 'instance_url' and 'access_token' but after performing a query request using those, i receive an "INVALID_SESSION_ID" error.

my authentication request code:

function get_sf_auth_data() {

	$post_data = array(
        'grant_type'    => 'password',
        'client_id'     => 'xxxxxxxxxxxxxxxxxxxxxx', //My client id (xxx... for this example)
        'client_secret' => '111111111111', // My client secret (111... for this example)
        'username'      => 'my_user_name',
        'password'      => 'my_user_password'
	);
	
	$headers = array(
    	'Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'
	);

	$curl = curl_init('https://login.salesforce.com/services/oauth2/token');
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_POST, true);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

	$response = curl_exec($curl);
	curl_close($curl);

    // Retrieve and parse response body
    $sf_access_data = json_decode($response, true);
    
    echo '*********' . $sf_response_data['instance_url'] . '********';
    echo '*********' . $sf_response_data['access_token'] . '********';

    return $sf_access_data;
}
My query request code:
function get_user_sfid($sf_access_data, $user_id_number){

	$sql = "SELECT Id, Name 
			FROM Account 
			WHERE ID__c = '$user_id_number'";

	$url = $sf_access_data['instance_url'] . '/services/data/v20.0/query/?q=' . urlencode($sql);

	$headers = array(
		'Authorization' => 'OAuth ' . $sf_access_data['access_token']
	);

	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_HEADER, false);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

	$json_response = curl_exec($curl);
	curl_close($curl);

	var_dump($json_response); // This prints out the response where i got the error

	$response = json_decode($json_response);

	return $response['id'];
}
The response to the query request as is:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
I have also tried using this guide (http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php) as refrence but it uses an "authorization_code" authentication and not a password one.

 
Best Answer chosen by אביב קסוטו
אביב קסוטואביב קסוטו

I've found the solution! The way i was defining my "headers" array was wrong.

instead of defining it:

$headers = array(
	"Authorization" => "OAuth " . $sf_access_data['access_token']
);

I should have been defining it:

$headers = array(
	"Authorization: OAuth " . $sf_access_data['access_token']
);
I get my query results now and everything is working. Thanks for all the help :)

All Answers

om gupta(sfdc)om gupta(sfdc)
please check the connected app has full access
 
אביב קסוטואביב קסוטו
I have now set full access to the app, waited 15 minutes, and i still get the same error
om gupta(sfdc)om gupta(sfdc)
have you tried with rest client by google chrome . let check on that if that exception occur may be your version may be greater right now you  are using v20.0

 
אביב קסוטואביב קסוטו
Checked my api version and it is indeed "40.0" and not "20.0", i changed it in my code to "40.0" and tried from chrome as well as firefox. still the same error.
om gupta(sfdc)om gupta(sfdc)
i think may be username and password incorrect or (password + security token should be passed). plz login to check thats its working 
אביב קסוטואביב קסוטו
password+user_name+client_id+client_secret are all correct. As i mentioned in my original question i do get an access token in the authorization request which i wouldn't get if any of them were incorrect (which is also a test i performed for each of them so i know for sure).
om gupta(sfdc)om gupta(sfdc)
true you  are right:)it may not be an issue like that but it happend. i don't think you are doing any wrong . i am much familier to java not php so unable to describe more on that . but i used that things in java and its working.

 
אביב קסוטואביב קסוטו
Can you send me an example of what you did in java with the 'password' grant-type (in case you did) maybe i can compare, i know java too
אביב קסוטואביב קסוטו

I've found the solution! The way i was defining my "headers" array was wrong.

instead of defining it:

$headers = array(
	"Authorization" => "OAuth " . $sf_access_data['access_token']
);

I should have been defining it:

$headers = array(
	"Authorization: OAuth " . $sf_access_data['access_token']
);
I get my query results now and everything is working. Thanks for all the help :)
This was selected as the best answer
om gupta(sfdc)om gupta(sfdc)
ok great  welcome :)