我正在尝试在我的项目中实现 Facebook 登录,但是我收到一个错误:
Middleware.php 第 69 行中的 ClientException:客户端错误:400
在 /Applications/MAMP/htdocs/laratest/vendor/guzzlehttp/guzzle/src/Middleware.php 第 69 行
在 Promise.php 第 199 行的 Middleware::GuzzleHttp{closure}(object(Response))
在 Promise.php 第 152 行中的 Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null))
在 TaskQueue.php 第 60 行中的 Promise::GuzzleHttp\Promise{closure}()
在 CurlMultiHandler.php 第 96 行中的 TaskQueue->run()
我经历的步骤:
composer require laravel/socialite
&composer update
.在我的config>services.app中,
'facebook' => [
'client_id' => env('FB_CLIENT_ID'),
'client_secret' => env('FB_SECRET_ID'),
'redirect' => 'http://localhost.com:8888',
],
在config>app.php 中添加
Laravel\Socialite\SocialiteServiceProvider::class,
&'Socialite' => Laravel\Socialite\Facades\Socialite::class,
成功设置路由。
Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider');
Route::get('auth/facebook', 'Auth\AuthController@handleProviderCallback');
在我的刀片文件中成功设置链接。
Login with Facebook
在我的Controller>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(); }); }
编辑:
当我注释掉$user = Socialite::driver('facebook')->user();
部分时,我被重定向到localhost.com/auth/facebook
编辑2:
我的.env
文件:
'facebook' => [
FB_CLIENT_ID => '###',
FB_SECRET_ID => 'this-is-secret!',
],