salesforce rest api 使用访问令牌时出现无效会话 ID 错误
salesforce rest api invalid session id error when using access token
我正在尝试使用 PHP 中的 REST api 和 CURL 从 salesforce 检索数据。
我执行身份验证请求并收到 'instance_url' 和 'access_token' 但在使用它们执行查询请求后,我收到 "INVALID_SESSION_ID" 错误。
我的认证请求码:
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;
}
我的查询请求码:
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'];
}
查询请求的响应原样:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
我也尝试过使用本指南 (http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php) 作为参考,但它使用 "authorization_code" 身份验证而不是密码。
编辑
该应用在 salesforce 中具有完全访问权限,api 版本和身份验证数据都正确
我找到了解决方案。
我将 headers 数组定义为:
$headers = array(
"Authorization" => "OAuth " . $sf_access_data['access_token']
);
当我应该将其定义为:
$headers = array(
"Authorization: OAuth " . $sf_access_data['access_token']
);
我正在尝试使用 PHP 中的 REST api 和 CURL 从 salesforce 检索数据。
我执行身份验证请求并收到 'instance_url' 和 'access_token' 但在使用它们执行查询请求后,我收到 "INVALID_SESSION_ID" 错误。
我的认证请求码:
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;
}
我的查询请求码:
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'];
}
查询请求的响应原样:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
我也尝试过使用本指南 (http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php) 作为参考,但它使用 "authorization_code" 身份验证而不是密码。
编辑
该应用在 salesforce 中具有完全访问权限,api 版本和身份验证数据都正确
我找到了解决方案。
我将 headers 数组定义为:
$headers = array(
"Authorization" => "OAuth " . $sf_access_data['access_token']
);
当我应该将其定义为:
$headers = array(
"Authorization: OAuth " . $sf_access_data['access_token']
);