laravel Facebook SDK异常:跨站点请求伪造验证失败,缺少必需的参数“state”

koaltpgm  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(116)

我正在使用laravel 8,我想尝试通过Facebook登录,但当我尝试以下代码时,我从SDK得到一个异常:«跨站点请求伪造验证失败。缺少必需的参数“state”。».我知道这个问题已经被问过好几次了,但我仍然有困难
这是我的代码

Facebook Controller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Facebook;

class FacebookController extends Controller
{
    //
    protected $fb;

    public function __construct(Facebook $fb)
    {
        $this->fb = $fb;
    }

    public function redirectToFacebook()
    {
        session_start();
        $helper = $this->fb->getRedirectLoginHelper();
        $loginUrl = $helper->getLoginUrl(route('login.facebook.callback'));

        return redirect()->to($loginUrl);
    }

    public function handleFacebookCallback()
    {
        session_start();
        $helper = $this->fb->getRedirectLoginHelper();

        try {
            $accessToken = $helper->getAccessToken();
        } catch (FacebookSDKException $e) {
            // Handle exception
            return redirect()->route('facebook')->with('error', 'Facebook SDK Exception: '.$e->getMessage());
        }

        if (!isset($accessToken)) {
            if ($helper->getError()) {
                // Handle error
                return redirect()->route('facebook')->with('error', 'Facebook SDK Error: '.$helper->getError());
            }

            return redirect()->route('facebook')->with('error', 'Facebook Login Error');
        }

        // Logged in
        $oAuth2Client = $this->fb->getOAuth2Client();
        $tokenMetadata = $oAuth2Client->debugToken($accessToken);

        // Retrieve user details from the Facebook Graph API
        try {
            $user = $this->fb->get('https://58b2-202-158-12-52.ngrok-free.app/', $accessToken)->getGraphUser();
            $name = $user->getName();
            $facebookId = $user->getId();
            $email = $user->getEmail();

            return redirect()->route('facebook.index')->with('success', 'Successfully logged in with Facebook');
        } catch (FacebookSDKException $e) {
            return redirect()->route('facebook')->with('error', 'Facebook SDK Exception: '.$e->getMessage());
        }
    }

    public function index(){
        return view('facebook.login');
    }

路线

Route::get('facebook-login', [FacebookController::class, 'index'])->name('facebook');
Route::get('login/facebook', [FacebookController::class, 'redirectToFacebook'])->name('login.facebook');
Route::get('login/facebook/callback', [FacebookController::class, 'handleFacebookCallback'])->name('login.facebook.callback');

表单facebook.blade.php

@if (session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif

@if (session('error'))
    <div class="alert alert-danger">
        {{ session('error') }}
    </div>
@endif

<form action="{{ route('login.facebook') }}" method="GET">
    @csrf
    <button type="submit">Login with Facebook</button>
</form>
kkbh8khc

kkbh8khc1#

您可以使用Laravel Socialite包,

public function redirectToFacebook()
{
    return Socialite::driver('facebook')->redirect();
}

public function handleFacebookCallback()
{
    $fbUser = Socialite::driver('facebook')->user();
    // Now do your other staff here
}

注意:你必须正确设置你的Facebook应用程序,并需要小心使用凭据。

相关问题