Laravel Socialite Facebook 错误:“POST https://graph.facebook.com/oauth/access_token 导致‘400 错误请求’"

2021-12-31 00:00:00 oauth php facebook facebook-graph-api

我已经使用 Laravel Socialite 为 Facebook 进行了社交登录,并且运行良好.我没有改变任何东西,现在它不起作用.尝试登录时显示此错误:

I've made social login using Laravel Socialite for facebook and it was working fine. I haaven't changed anything and now it's not working. It shows this error when trying to login:

客户端错误:POST https://graph.facebook.com/oauth/access_token导致400 Bad Request响应:{"error":{"message":"此 IP 无法向该应用程序发出请求.","type":"OAuthException","code":5,"fbtrace_id":"D(已截断...)

Client error:POST https://graph.facebook.com/oauth/access_tokenresulted in a400 Bad Requestresponse: {"error":{"message":"This IP can't make requests for that application.","type":"OAuthException","code":5,"fbtrace_id":"D (truncated...)

我没有更改 Facebook 应用程序中的设置,也没有更改我的代码.我的代码如下:

I haven't changed setting in my facebook app, nor in my code. My code is the following:

Route::get('login', ['as' =>'getLogin', 'uses'=>'AuthAuthController@getLogin']);
Route::get('handleProviderCallback/{provider}', 'AuthAuthController@handleProviderCallback');

public function login($provider = false)
    {
     if(!$provider) {
            $user = Input::all();
            $auth_user = new AuthenticateUser();
            $is_logged = $auth_user->findByEmailOrCreate($user, false);
            if($is_logged) {

                Session::put('user', Auth::user());
                Session::put('user_id', Auth::user()->id);
                Session::put('email', Auth::user()->email);
                Session::put('name', Auth::user()->name);

                return redirect()->intended('dashboard');
            }
            return redirect('/')->withInput()->with('error', 'Wrong username or password!');
        }
        return Socialite::with('facebook')->redirect();
    }
    
    public function handleProviderCallback($provider=false) {
        if(Input::has('code')) {
            $user = Socialite::with($provider)->user();
            $auth_user = new AuthenticateUser();
            $provider_user = $auth_user->findByEmailOrCreate($user, $provider);
            Session::put("user", array($provider_user));
            Session::put('user_id', $provider_user->id);
            Session::put('email',$provider_user->email);
            Session::put('name',$provider_user->name);

            return redirect('dashboard');
        }
    }

问题出现在这一行的 handleProviderCallback 方法中:

Problem appears in handleProviderCallback method in this line:

$user = Socialite::with($provider)->user();.

当我只转储这个时:$user = Socialite::with($provider)

它显示数据,但是当它像这样用来获取用户数据时,它返回错误:

it shows data but when it is used like that to get user data it returns the error:

$user = Socialite::with($provider)->user();

config/services.php 中,我设置了如下示例:

In config/services.php I've set settings like this example:

  'facebook' => [
        'client_id' => 'my-client-id-from-fb-app', 
        'client_secret' => 'my-secret-code-from-fb-app', 
        'redirect' => 'http://my-example-domain/my-site/handleProviderCallback/facebook'
    ],

在我的 facebook 应用程序中,我在设置中进行了设置:

In my facebook app I've set in settings:

应用域:my-example-domain,隐私政策网址:http://my-example-domain/my-site/,网站网址:http://my-example-domain/my-site/handleProviderCallback/facebook,有效的 OAuth 重定向 URI:http://my-example-domain/my-site/handleProviderCallback/facebook

推荐答案

是的,我根本没有检查请求以查看返回的内容,但是 Larachat 上有人帮我解决了这个问题.

Yeah, I didn't check the request at all to see what's returned but someone on Larachat helped me solve this problem.

在获取用户并处理请求之前,我所要做的就是在回调方法中添加以下代码:

All I had to do before getting the user and handle the request was to add the following code in the callback method:

if (!$request->has('code') || $request->has('denied')) {
   return redirect('/');}

相关文章