Laravel Socialite Facebook error: "POST https://graph.facebook.com/oauth/access_token resulted in a `400 Bad Request` "

12,825

Solution 1

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('/');}

Solution 2

I had this message on my local machine after getting secret key value from .env file

ClientException in RequestException.php line 111:
enter code here Client error: `POST 
https://graph.facebook.com/v2.8/oauth/access_token` resulted in a `400 
Bad Request` response:
{"error":{"message":"Error validating client 
secret.","type":"OAuthException","code":1,"fbtrace_id":"ID"}}

As expected problem was with secret key, I pasted it wrong, so after double check and right copy/past from fb everything worked.

Hope this info will help somebody.

Share:
12,825
ivva
Author by

ivva

Updated on June 05, 2022

Comments

  • ivva
    ivva almost 2 years

    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:

    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...)

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

    Route::get('login', ['as' =>'getLogin', 'uses'=>'Auth\AuthController@getLogin']);
    Route::get('handleProviderCallback/{provider}', 'Auth\AuthController@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');
            }
        }

    Problem appears in handleProviderCallback method in this line:

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

    When I only dump this: $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();

    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'
        ],

    In my facebook app I've set in settings:

    App Domains: my-example-domain, Privacy Policy URL: http://my-example-domain/my-site/, Site URL: http://my-example-domain/my-site/handleProviderCallback/facebook, Valid OAuth redirect URIs: http://my-example-domain/my-site/handleProviderCallback/facebook

  • D T
    D T over 5 years
    it still not woking.
  • Jigs1212
    Jigs1212 over 5 years
    You should never edit Vendor files.
  • Ittikorn S.
    Ittikorn S. over 5 years
    @Jigs1212 If you have better solutions, please do share it would really help.
  • Vladd
    Vladd over 3 years
    that is not a solution, this will just cause further problems and will stop working on first composer update.