2016-01-21 43 views
1

Salesforce的REST API,使用PHP,INVALID_SESSION_ID后,我连我的web应用到Salesforce,并成功地紧跟在Web服务器的OAuth流中的所有步骤,得到的access_token和相关信息: https://developer.salesforce.com/docs/atlas.en-us.api_placeorder.meta/api_placeorder/intro_understanding_web_server_oauth_flow.htm成功认证

似乎每走一步返回文档中指定的预期结果集,但是当我尝试使用最终访问令牌发出请求时,出现错误,说明我的会话ID无效。这里是我的代码:

// Execute a request for access_token and related info 
    $output = json_decode(curl_exec($ch)); 
    curl_close($ch); 
    var_dump($output); 

    // Extract token and instance_url from output 
    $sf_url = $output->instance_url . '/services/data/v35.0/'; 
    $sf_auth = 'Bearer ' . $output->access_token; 

    // Execute a new cURL request with auth values 
    $ci = curl_init(); 
    curl_setopt($ci, CURLOPT_URL, $sf_url); 
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ci, CURLOPT_HTTPHEADER, array(
     'Accept' => '*/*', 
     'Authorization' => $sf_auth, 
     'X-PrettyPrint' => 1 
    )); 

    $output2 = curl_exec($ci); 
    curl_close($ci); 
    var_dump($output2); 

从$输出转储给出:

object(stdClass)#194 (7) { 
["access_token"]=> string(112) "xxxxxxxxxxxxxxxx" 
["signature"]=> string(44) "xxxxxxxxxxxxxx" 
["scope"]=> string(3) "api" 
["instance_url"]=> string(27) "https://cs10.salesforce.com" 
["id"]=> string(68) "https://test.salesforce.com/id/xxxxx/xxxxx" 
["token_type"]=> string(6) "Bearer" 
["issued_at"]=> string(13) "1453358xxxxxx" } 

转储从$输出2给出了:

string(75) "[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]" 

这里任何帮助将非常感激。

* UPDATE *

我也尝试使用用户名,密码,验证流程,并且我得到了相同的结果。我可以获取访问令牌,但我无法查询数据库,并且返回的错误与INVALID_SESSION_ID相同。

我使用邮递员插件进行chrome传递所有相同的用户名 - 密码流信息,并且它用于进行查询。我不确定正在做什么不同,似乎所有传递的数据都是一样的。

回答

0

我找到了答案,我错误地设置了卷曲标题。我尝试将值作为关联数组传递,但正确的方法是这样的:

curl_setopt($ci, CURLOPT_HTTPHEADER, array(
    'Accept: application/json', 
    'Authorization:' . $sf_auth, 
    'X-PrettyPrint:1' 
));