Facebook SDK返回错误:跨站点请求伪造验证失败。 URL和会话中的状态参数不匹配

时间:2016-03-31 14:36:22

标签: facebook-graph-api sdk facebook-php-sdk laravel-5.2

我正在使用Laravel 5.2并且我正在使用SammyK / laravel-facebook-sdk Package i尝试了以下链接中的代码enter link description here

我一直有这样的错误:“跨站点请求伪造验证失败。来自URL和会话的”状态“参数不匹配。”

这是我的控制器代码:

public function callbackfbsdk(Facebook $fb){


$helper = $fb::getRedirectLoginHelper();

$loginUrl = $helper->getLoginUrl(['email']);

var_dump($loginUrl);

try {
    $token = $fb::getAccessTokenFromRedirect();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    dd($e->getMessage());
}

if (! $token) {
    // Get the redirect helper
    $helper = $fb::getRedirectLoginHelper();

    if (! $helper->getError()) {
        abort(403, 'Unauthorized action.');
    }

    // User denied the request
    dd(
        $helper->getError(),
        $helper->getErrorCode(),
        $helper->getErrorReason(),
        $helper->getErrorDescription()
    );
}

if (! $token->isLongLived()) {
    $oauth_client = $fb::getOAuth2Client();

    try {
        $token = $oauth_client->getLongLivedAccessToken($token);
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        dd($e->getMessage());
    }
}

$fb::setDefaultAccessToken($token);

Session::put('fb_user_access_token', (string) $token);

try {
    $response = $fb::get('/me?fields=id,name,email');
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    dd($e->getMessage());
}

$facebook_user = $response->getGraphUser();

$user = App\User::createOrUpdateGraphNode($facebook_user);

// Log the user into Laravel
Auth::login($user);

return redirect('/')->with('message', 'Successfully logged in with Facebook');

}

关于如何解决此错误的任何想法?

1 个答案:

答案 0 :(得分:0)

为什么要将这些行放在回调中

$helper = $fb::getRedirectLoginHelper();

$loginUrl = $helper->getLoginUrl(['email']);

它会尝试在redirect.seperate之后再次生成链接,将解决问题