LinkedIn API OAuth2 - 无法检索访问令牌

时间:2016-12-19 00:48:02

标签: php curl oauth-2.0 linkedin

我正在尝试在LinkedIn API上获取访问令牌,但是我收到以下错误:

{"error":"invalid_request","error_description":"Unable to retrieve access token: appid or redirect uri does not match authorization code or authorization code expired"}

PHP代码:

获取授权码(工作):

$linkedIn = array(
        'clientId' => 'My id',
        'redirectUri' => WEBSITE.'User/liCallback/',
        'state' => 'rz5t4er6t7re68t7e6rt6ze9',
        'scope' => 'r_basicprofile'
    );
    $liLoginUrl = 'https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id='.$linkedIn['clientId'].'&redirect_uri='.$linkedIn['redirectUri'].'?state='.$linkedIn['state'].'&scope='.$linkedIn['scope'];

获取访问toekn(不起作用):

 public function liCallback(){
  $linkedin =   array(
      'grant_type' => 'authorization_code',
      'code' => $this->get('code'),
      'redirectUri' => WEBSITE.'User/liCallback/',
      'client_id' => 'My id',
      'client_secret' => 'My client secret'
  );



  $curl_handle=curl_init();
  curl_setopt($curl_handle, CURLOPT_URL,'https://www.linkedin.com/oauth/v2/accessToken');
  curl_setopt($curl_handle, CURLOPT_POST, 1);
  echo curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'grant_type='.$linkedin['grant_type'].'&code='.$this->get('code').'&redirect_uri='.urlencode($linkedin['redirectUri']).'&client_id='.$linkedin['client_id'].'&client_secret='.$linkedin['client_secret']);
  curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl_handle, CURLOPT_USERAGENT, 'x-www-form-urlencoded');
  $query = curl_exec($curl_handle);
  var_dump($query);
  curl_close($curl_handle);

}

我尝试使用encodeurl()但是我得到了同样的错误。 我尝试使用file_get_content()但是我遇到了400错误。

感谢您的帮助!

0 个答案:

没有答案