2013-05-06 193 views
3

我目前无法取回LinkedIn令牌。LinkedIn获取令牌返回HTTP 1.1 400 - 错误请求错误

我目前正在关注developers documentation in order to get through authentication

我还使用this sample作为基础代码

然而,当我通过授权过程得到的,我在其中得到AUTH码,得到一个令牌的用户的过程中失败,用错误的请求错误(400)

下面是从样本页面修改后的代码 (注:的注释部分我们以前attemps我做了,动卷曲前)

public function getAccessToken() { 
     $params = array('grant_type' => 'authorization_code', 
      'client_id' => API_KEY, 
      'client_secret' => API_SECRET, 
      'code' => $_GET['code'], 
      'redirect_uri' => REDIRECT_URI.'/linkedin/auth', 
     ); 


     // Access Token request 
     $url = urldecode('https://www.linkedin.com/uas/oauth2/accessToken?'.http_build_query($params)); 
     //header("Content-length: ".strlen($url)); 
     // Tell streams to make a POST request 


     echo $url; 

     /*$context = stream_context_create(
      array('http' => 
      array('method' => 'POST', 
      ) 
      ) 
     );*/ 

     /*$context = stream_context_create(
         array('http' => 
          array('method' => 'POST', 
            'header' => 'Content-type: application/x-www-form-urlencoded', 
            'content' => http_build_query($params)) 
         ) 
        ); 

     // Retrieve access token information 
     $response = file_get_contents($url, FALSE, $context);*/ 


     $ch = curl_init(); 

     curl_setopt($ch,CURLOPT_URL, $url); 
     curl_setopt($ch,CURLOPT_POST,1); 
     $response = curl_exec($ch); 

     curl_close($ch); 

     $token = json_decode($response); 

     echo json_decode($response); 

     // Store access token and expiration time 
     $_SESSION['access_token'] = $token->access_token; // guard this! 
     $_SESSION['expires_in'] = $token->expires_in; // relative time (in seconds) 
     $_SESSION['expires_at'] = time() + $_SESSION['expires_in']; // absolute time 

     return true; 
} 

忘记提到的是,echo $url;部分,输出的是,当在浏览器中手动地插入,生成令牌

+0

你尝试'urlencode'代码和参数REDIRECT_URI? – 2013-05-06 18:23:48

+0

@Yan是的,我做到了。结果是一样的。在请求获取响应时出现错误的请求,但在浏览器中手动插入URL会按预期工作,即带有过期时间戳和访问令牌的JSON响应。 – 2013-05-06 18:25:07

+2

您是否试图以'GET'而不是'POST'发送? – 2013-05-06 18:28:09

回答

3

有效的用户为了使工作请求的URL,则必须在GET被发送,而不是POST

删除此行:

curl_setopt($ch,CURLOPT_POST,1);