linkedin oauth无法交换访问令牌

时间:2016-08-04 17:07:13

标签: php api curl oauth linkedin-api

我正在尝试使用linkedin来获取用户的个人资料数据,我从登录表单上的按钮获取授权代码,该按钮重定向到另一个页面,我想要一些字段填充其名称,以便创建用户或登录。我无法与我的帖子交换身份验证令牌。我收到了错误的请求或未经授它说我错过了所需的参数" client_id",但显然有......请看看,谢谢。

$code = $_GET['code'];
$data =array(
"grant_type" =>"authorization_code",
"code"=> $code,
"redirect_uri" => "http:\\localhost\index.php?doctor=linkedin",
"client_id" => "shh",
"client_secret" => "secret"
 );

$str_data = json_encode($data);

//echo $str_data;


//function sendPostData($url, $post){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.linkedin.com/oauth/v2/accessToken');  
 curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "HandleHeaderLine");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);

 $token = json_decode($result);
 var_dump($token);

  curl_close($ch);  // Seems like good practice

  function HandleHeaderLine( $curl, $header_line ) {
   echo "<br>".$header_line; // or do whatever
   return strlen($header_line);
 }

1 个答案:

答案 0 :(得分:1)

LinkedIn希望将POSTed字段作为查询字符串。尝试更改:

$str_data = json_encode($data);

为:

$str_data = http_build_query($data);

来自documentation

的示例
POST /oauth/v2/accessToken HTTP/1.1
Host: www.linkedin.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=987654321&redirect_uri=https%3A%2F%2Fwww.myapp.com%2Fauth%2Flinkedin&client_id=123456789&client_secret=shhdonottell

希望有所帮助。