2017-07-17 11 views
0

PHPのREST APIおよびCURLを使用してsalesforceからデータを取得しようとしています。salesforce rest apiアクセストークンを使用したときに無効なセッションIDエラーが発生しました

私は認証要求を実行し、 '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」認証を使用します。私は解決策を見つけた

EDIT

アプリは、Salesforceのフルアクセスを持ち、両方のAPIバージョンと認証データが正しい

答えて

0

です。

私のように私のヘッダの配列を定義した:

$headers = array(
    "Authorization: OAuth " . $sf_access_data['access_token'] 
); 
:私のようにそれを定義している必要があります場合は

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

関連する問題