我目前在获取LinkedIn令牌时遇到问题.
我目前正在追踪developers documentation in order to get through authentication.
我也使用this sample作为基本代码
但是,经过授权过程(其中获得身份验证码)之后,获取用户令牌的过程失败,并出现错误请求错误(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;部分,输出一个网址,该网址在手动插入浏览器中时会生成有效的用户令牌
解决方法:
为了使请求正常工作,必须以GET而不是POST的形式发送请求.
删除此行:
curl_setopt($ch,CURLOPT_POST,1);