2013-05-06 246 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); 
相關問題