Laravel 5.1 - Facebook Authentication through Socialite

I try to implement Facebook Login for my project, but I get an error message:

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

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

  • in Middleware :: GuzzleHttp {clos} (object (response)) in Promise.php line 199

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

  • in Promise :: GuzzleHttp \ Promise {close} () in the line TaskQueue.php 60

  • in TaskQueue-> run () in the line CurlMultiHandler.php 96


The steps that I went through:

  • composer require laravel/socialiteand composer update.

  • In my configuration> services.app

'facebook' => [ 'client_id' => env('FB_CLIENT_ID'), 'client_secret' => env('FB_SECRET_ID'), 'redirect' => 'http://localhost.com:8888', ],

  • Added Laravel\Socialite\SocialiteServiceProvider::class,and 'Socialite' => Laravel\Socialite\Facades\Socialite::class,in the config> app.php

  • Set up routes successfully.

Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider'); Route::get('auth/facebook', 'Auth\AuthController@handleProviderCallback');

  • . Login with Facebook

  • > AuthController.php :

    use Laravel\Socialite\Facades\Socialite;
    
    ** Beside everything that AuthController has, inside the AuthController class, I added:**
    
      public function redirectToProvider()
       {
        return Socialite::driver('facebook')
             ->scopes(['scope1', 'scope2'])->redirect();
       }
    
    /**
     * Obtain the user information from Facebook.
     *
     * @return Response
     */
    
       public function handleProviderCallback()
        {
          $user = Socialite::driver('facebook')->user();
    
        // $user->token;
        }
    
  • :

       public function up()
        {
           Schema::create('users', function (Blueprint $table) {
              $table->increments('id');
              $table->string('username');
              $table->string('name');
              $table->string('email')->unique();
              $table->string('password', 60);
              $table->string('avatar');
              $table->string('provider');
              $table->string('provider_id');
              $table->rememberToken();
              $table->timestamps();
                });
        }
    

Edit: $user = Socialite::driver('facebook')->user();, localhost.com/auth/facebook

2: .env :

'facebook' => [ FB_CLIENT_ID => '###', FB_SECRET_ID => 'this-is-secret!', ],

+4
4

, URL-, Laravel , .

Facebook ? code, ! , Socialite, , Facebook.

, 1 2, Facebook . , public_profile email, . ( - , )

+3

, Laravel

php artisan config:clear

.env FB_CLIENT_ID FB_SECRET_ID. , , facebook , .

+1

(ClientException Middleware.php 69: : 400). " " facebook, . , .

0

I had a similar problem with Facebook APi - it gave a 400 response code and I just tried:

composer requires laravel / socialite and composer updates

and he updated dependencis:

Dependency updates (including require-dev) - Uninstall laravel / socialite (v2.0.11) - Install laravel / socialite (v2.0.14)

Im Using Laravel 5.1:

composer.json:

    "laravel/framework": "5.1.*",
    "laravel/socialite": "^2.0"

And now it works fine

Hope this helps someone

0
source

Source: https://habr.com/ru/post/1599001/


All Articles