2016-12-19 3 views
0

アクセストークンを取得することができません私はLinkedInのAPIのアクセストークンを取得しようとしているが、私は次のエラーを取得:LinkedInのAPIのOAuth2 -

{"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

誰かが私を助けることができますか? – mdck

+0

アップ?誰かが私を助けることができますか? – mdck

+0

'file_get_content()'はリンクされていないので、 'file_get_content()'で設定できない余分なヘッダーが必要です。 –

答えて

0

下のコードでは、コンテンツタイプにCURLOPT_USERAGENTを使用していますが、linkedInではヘッダにContent-Typeというように渡す必要があります。 参照形式https://developer.linkedin.com/docs/oauth2 enter image description here

$curl = curl_init('https://www.linkedin.com/oauth/v2/accessToken'); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'HOST: www.linkedin.com', 
    'Content-Type: application/x-www-form-urlencoded' 
)); 
curl_setopt($curl, CURLOPT_POSTFIELDS, 
    http_build_query(array(
    'client_id' => $linkedin['client_id'], 
    'redirect_uri' => WEBSITE.'User/liCallback/', 
    'client_secret' => $linkedin['client_secret'], 
    'code' => $this->get('code'), 
    'grant_type' => 'authorization_code' 
))); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$auth = curl_exec($curl); 
$secret = json_decode($auth); 
if (isset($secret->access_token)) { 
    echo $access_token = $secret->access_token; 
} 
+0

私はあなたの解決策を試しましたが、うまくいきません。私は同じエラーが発生します。 – mdck

+0

あなたはどんなエラーが表示されますか? –

+0

私は同じエラーが発生します.. – mdck