Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 536
ivva Avatar asked Feb 26 '17 11:02

ivva


1 Answers

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('/');}
like image 65
Raj Kumar Avatar answered Oct 01 '22 23:10

Raj Kumar