Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 - Facebook through Socialite (Client Error: 400)

Using this tutorial on Laracast (Laravel 5.0 - Socialite), specifically until min 12.11, I have successfully setup everything. However, I am using Laravel 5.1

  • Defined my route but currently commented out the callback provider, as I am just trying to fetch the user details through taken token:

    Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider');
    //Route::get('auth/facebook/aaa', 'Auth\AuthController@handleProviderCallback');
    
  • Added necessities in config>services.php:

    'facebook' => [
     'client_id' => '##',
     'client_secret' => env('FB_SECRET_ID'),
     'redirect' => 'http://laratest.dev/auth/facebook',   
    ],
    

I decided to return to the same page (auth/facebook) for now. That's why I have set the return as domain.devv/auth/facebook for testing.

  • In my AuthController.php, I set:

    public function redirectToProvider(AuthenticateUser $authenticateUser, Request $request)
     {  
       return $authenticateUser->execute($request->has('code'));
     }
    
  • And finally, in my AuthenticateUser.php:

    use Illuminate\Contracts\Auth\Guard;  (PS. I changed Authenticator to Guard)
    
       class AuthenticateUser {
          private $users;
          private $socialite;
          private $auth;
    
    public function __construct(UserRepository $users, Socialite $socialite, Guard $auth)
     {
         $this->users = $users;
         $this->socialite = $socialite;
         $this->auth = $auth;
     } 
    
    public function execute($hasCode)
     {   
       // dd($hasCode) *First dd
    
      if ( ! $hasCode) return $this->getAuthorizationFirst();
    
      // $user = $this->socialite->driver('facebook')->user();
    
      // dd($hasCode) *Second dd
      // dd($user) *Third dd
     }
    
    private function getAuthorizationFirst()
     {
       return $this->socialite->driver('facebook')
         ->scopes(['public_profile', 'email'])->redirect();
     } 
    }
    

Now, I am clicking Login with Facebook link and getting directed to domain.dev/auth/facebook?code=943299043290...

When I use only the *First dd (with all the other commented lines are commented)- it returns false.

When I use the only the *Second dd (with commenting the line $user = $this->socialite->driver('facebook')->user();), it returns true.

So everything is working perfect and goes through execute(), then through getAuthorizationFirst(). Finally, returns back to execute() with the taken token which is also contained in the link (domain.dev/auth/facebook?code=943299043290...).


My problem arises here:

At the second I am uncommenting $user = $this->socialite->driver('facebook')->user();, I am getting an error:

ClientException in Middleware.php line 69: Client error: 400

1. in /Applications/MAMP/htdocs/laratest/vendor/guzzlehttp/guzzle/src/Middleware.php line 69

2. at Middleware::GuzzleHttp{closure}(object(Response)) in Promise.php line 199

3. at Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) in Promise.php line 152

4. at Promise::GuzzleHttp\Promise{closure}() in TaskQueue.php line 60

5. at TaskQueue->run() in CurlMultiHandler.php line 96

this line ($user = $this->socialite->driver('facebook')->user();) is not working for me for some reasons (while it does in the tutorial (min 12.11 for 15-second brief explanation).

Whenever I use $user = $this->socialite->driver('facebook')->user();, I receive the error ClientException in Middleware.php line 69: Client error: 400 and, of course, I can't get dd($user).

To add, when I see the ClientException error, the link is still with the code: (domain.dev/auth/facebook?code=943299043290...)

like image 473
senty Avatar asked Oct 30 '22 23:10

senty


1 Answers

i was having this issue too, in my case i have to add my domain in config/session.php in the domain field and add it too in my app provider

like image 85
Andres Felipe Avatar answered Nov 15 '22 05:11

Andres Felipe