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

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

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

回答1:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!