使用LinkedIn访问令牌时出错

时间:2015-03-12 13:51:51

标签: php http post curl linkedin

经过多次测试后,我无法检索访问令牌。它每次都给我这个错误:

"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : authorization code not found","error":"invalid_request"

我完全心烦意乱。几天我无法解决我的问题。

以下是我的申请代码:

Route::get('login/linkedin', function()
{
$lk_credentials = Config::get('linkedin.public0');
$provider = new LinkedIn($lk_credentials);
if(!Input::has('code')){
    $provider->authorize();
}else{
    $code = Input::get('code');
    if(strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with LinkedIn');
    try{
        $params = array(
            'response_type' => 'code',
            'client_id' => '*************',
            'redirect_uri' => urlencode(url('login/linkedin')),
            'state' => $_GET['state'],
        );
        $postdata = http_build_query($params);
        $url = '/uas/oauth2/authorization?'.$postdata;
        $context = stream_context_create(array('https' => array('method' => 'GET')));
        $t = $provider->getAccessToken('authorization_code', array('code' => $_GET['code']));
        Session::put('oauth2_access_token', $t->accessToken);session(['oauth2_access_token' => $t->accessToken]);
        try{
            if(count($_POST) > 0){
                print_r($_POST);
                exit('post');
            }
            $params = array(
                'grant_type' => 'authorization_code',
                'code' => Session::get('oauth2_access_token'), //$_GET['code'],
                'redirect_uri' => urlencode(url('login/linkedin')),
                'client_id' => '123456789',
                'client_secret' => '123456789',
            );
            $postdata = http_build_query($params);
            $c = curl_init();
            curl_setopt($c, CURLOPT_URL, 'https://www.linkedin.com/uas/oauth2/accessToken');
            curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($c, CURLOPT_POST,true);
            curl_setopt($c, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($c, CURLOPT_POSTFIELDS,$postdata);
            curl_setopt($c, CURLOPT_HEADER, false);
            curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded',));
            $output = curl_exec($c);
            if($output === false){
                trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);
                exit('Erreur');
            }else{
                var_dump($output);
                //exit('Affiche');
            }
            curl_close($c);
        }catch(Exception $e){
            return 'Unable to get Request Token';
        }
    }catch(Exception $e){
        return 'Unable to get user authorization_code';
    }
    try{
        $resource = '/v1/people/~:(id,emailAddress,firstName,lastName,pictureUrl,dateOfBirth,location)';
        $params = array(
            'oauth2_access_token' => Session::get('oauth2_access_token'), 
            'format' => 'json',
        );
        $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
        $context = stream_context_create(array('http' => array('method' => 'GET')));
        $response = file_get_contents($url, false, $context);
        $data = json_decode($response);
        Session::put('data', $data);session(['data' => $data]);
        return redirect('/')->with('data',$data);
    }catch(Exception $e){
        return 'Unable to get user details';
    }
}

});

有人可以帮助我吗?

供参考;我使用Laravel5,身份验证是OAuth2。

提前谢谢大卫。

0 个答案:

没有答案