Using Laracast 上的本教程 https://laracasts.com/series/whats-new-in-laravel-5/episodes/9(Laravel 5.0 - 社交名流),特别是直到12.11 分钟,我已经成功设置了一切。但是,我使用的是 Laravel 5.1
-
定义了我的路线,但目前注释掉了回调提供程序,因为我只是尝试通过获取的令牌获取用户详细信息:
Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider');
//Route::get('auth/facebook/aaa', 'Auth\AuthController@handleProviderCallback');
-
添加必需品于config>services.php
:
'facebook' => [
'client_id' => '##',
'client_secret' => env('FB_SECRET_ID'),
'redirect' => 'http://laratest.dev/auth/facebook',
],
我决定返回同一页面(auth/facebook
) 目前。这就是为什么我将回报设置为domain.devv/auth/facebook
供测试用。
-
In my AuthController.php
, I set:
public function redirectToProvider(AuthenticateUser $authenticateUser, Request $request)
{
return $authenticateUser->execute($request->has('code'));
}
-
最后,在我的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();
}
}
现在,我点击Login with Facebook
链接并定向至domain.dev/auth/facebook?code=943299043290...
当我只使用*First dd
(所有其他注释行都被注释)-它返回false
.
当我只使用*Second dd
(注释行$user = $this->socialite->driver('facebook')->user();
),它返回true
.
所以一切都很完美并且顺利进行execute()
,然后通过getAuthorizationFirst()
。最后,返回到execute()
所获取的令牌也包含在链接中(domain.dev/auth/facebook?code=943299043290...
).
我的问题出现在这里:
第二次我取消评论$user = $this->socialite->driver('facebook')->user();
,我收到错误:
Middleware.php 第 69 行中的 ClientException:客户端错误:400
1.在/Applications/MAMP/htdocs/laratest/vendor/guzzlehttp/guzzle/src/Middleware.php第69行
2. 在 Promise.php 中的 Middleware::GuzzleHttp{closure}(object(Response)) 第 199 行
3. 在 Promise.php 中的 Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) 第 152 行
4. 在 TaskQueue.php 中的 Promise::GuzzleHttp\Promise{closure}() 第 60 行
5. 在 CurlMultiHandler.php 第 96 行的 TaskQueue->run() 处
这条线($user = $this->socialite->driver('facebook')->user();
) is 不工作对我来说,出于某些原因(虽然它确实在tutorial https://laracasts.com/series/whats-new-in-laravel-5/episodes/9 (分钟 12.11 15 秒简短解释).
每当我使用$user = $this->socialite->driver('facebook')->user();
,我收到错误ClientException in Middleware.php line 69: Client error: 400
当然,我无法得到dd($user)
.
要添加的是,当我看到 ClientException 错误时,链接仍然包含代码:(domain.dev/auth/facebook?code=943299043290...
)