3

我目前无法获取 LinkedIn 令牌。

我目前正在关注开发人员文档以通过身份验证

我也将此示例用作基本代码

但是,在我通过授权过程(在该过程中获取身份验证码)后,为用户获取令牌的过程失败,并出现错误请求错误 (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;部分输出一个 url,当手动插入浏览器时,会生成一个有效的用户令牌

4

1 回答 1

3

为了使请求起作用,它必须被发送GET而不是POST.

删除这一行:

curl_setopt($ch,CURLOPT_POST,1);
于 2013-05-06T18:50:43.387 回答