LinkedIn获取令牌返回HTTP 1.1 400 - 错误请求错误

时间:2013-05-06 18:21:27

标签: php codeigniter oauth linkedin

我目前无法获取LinkedIn令牌。

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

我也使用this sample作为基本代码

但是,在我完成授权过程后,我获得了auth-code,为用户获取令牌的过程失败,出现错误请求错误(400)

以下是示例页面中的修改代码 (注意:我们之前尝试过的评论部分,在转移到cURL之前)

 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;部分,输出一个网址,当手动插入浏览器时,生成有效的用户令牌

1 个答案:

答案 0 :(得分:3)

要使请求生效,必须在GET而不是POST中发送。

删除此行:

curl_setopt($ch,CURLOPT_POST,1);