当我获得LinkedIn的访问令牌时得到401

时间:2014-06-14 05:13:26

标签: php oauth linkedin

$ oauth-> getAccessToken()导致无效的身份验证/错误请求(获得401,预期的HTTP / 1.1 20X或重定向)。

如何查看请求标头以确定哪些是错误的?

    $oauth = new OAuth(CONSUMER_KEY, CONSUMER_SECRET);

    $oauth->disableSSLChecks();
    $request_token_response = $oauth->getRequestToken('https://api.linkedin.com/uas/oauth/requestToken');

    if($request_token_response === FALSE) {
            throw new Exception("Failed fetching request token, response was: " . $oauth->getLastResponse());
    } else {
        $request_token = $request_token_response;
        var_dump($request_token);

        if (!isset($_GET['oauth_verifier'])) {
            $this->redirect("https://api.linkedin.com/uas/oauth/authorize?oauth_token=" . $request_token['oauth_token']);
        } else {
            $oauth_verifier = $_GET['oauth_verifier'];
            $oauth->setToken($request_token['oauth_token'], $request_token['oauth_token_secret']);

            $access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken';
            $access_token_response = $oauth->getAccessToken($access_token_url, "", $oauth_verifier);

            if($access_token_response === FALSE) {
                    throw new Exception("Failed fetching request token, response was: " . $oauth->getLastResponse());
            } else {
                $access_token = $access_token_response;

                $params = array();
                $headers = array();
                $method = OAUTH_HTTP_METHOD_GET;

                // Specify LinkedIn API endpoint to retrieve your own profile
                $url = "http://api.linkedin.com/v1/people/~";

                // By default, the LinkedIn API responses are in XML format. If you prefer JSON, simply specify the format in your call
                // $url = "http://api.linkedin.com/v1/people/~?format=json";

                // Make call to LinkedIn to retrieve your own profile
                $oauth->fetch($url, $params, $method, $headers);

                echo $oauth->getLastResponse();
            }
        }

    }       
}

1 个答案:

答案 0 :(得分:1)

oauth_verifier仅验证从中获取的request_token。我需要在会话中存储原始的request_token,而不是在回调时获取新的request_token。