【问题标题】:Laravel 5.1 - Socialite - Fetching UserLaravel 5.1 - 社交名流 - 获取用户
【发布时间】:2015-07-23 12:17:41
【问题描述】:

使用官方Laravel Docs 5.1 - Socialite,我设置了路由并编辑了我的AuthController。

   public function redirectToProvider()
   {  
      return Socialite::driver('facebook')
         ->scopes(['public_profile', 'email'])->redirect();
   }

完美运行并返回 http://domain.dev/auth/facebook/callback?code=AQDLFlYr...

然后我使用了Laracasts' Socialite - Laravel 5.0,我尝试将我的一个变成正确的 Facebook 身份验证。我在视频中做了所有事情,直到(min. 12.11,具体而言)。 (在 12.11 他在 15 秒内回顾了这些内容)。这就是我想要做的。


现在,当我将 AuthController 更改为:

  public function redirectToProvider(AuthenticateUser $authenticateUser, Request $request)
  {  
     return $authenticateUser->execute($request->has('code'));

  } 

...让我的AuthenticateUser class 像这样:

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();
    } 
  }

*UserRepository is currently empty.


当我使用*First dd 时,我会在屏幕上收到False。

当我使用*Second dd 时,我收到True。

当我使用*Third dd 时,我什么也没收到。

现在,在所有这些情况下,我都收到了http://domain.dev/auth/facebook?code=9329409329042。


编辑:

  • 我添加了return,现在链接包括?code=9390249032...,但是当我使用*Third dd - dd($user) 时,仍然没有返回任何内容

  • 我设法来到这里:

        public function execute($hasCode)
    {
    
             dd($hasCode); // returns FALSE now     
    if ( ! $hasCode) return $this->getAuthorizationFirst();
    
             dd($hasCode); // returns TRUE now
    
        $user = $this->socialite->driver('facebook')->user();
    
            dd($user);  // BUT STILL RETURNS NOTHING
    
     }
    

.. 链接包括?code=9943290...

  • 基本上,$user = $this->socialite->driver('facebook')->user(); 是不工作/转换的部分,因为 dd($user) 没有返回任何内容..

【问题讨论】:

    标签: facebook facebook-graph-api laravel laravel-socialite


    【解决方案1】:

    看来你可能忘记退货了。

    return $authenticateUser->execute($request->has('code'));
    

    【讨论】:

    • 你是对的。代码现在通过execute 和getAuthorizationFirst 工作,链接显示/?code=23193291... 但是,当我使用dd($user)(*第三dd)时,没有任何返回
    猜你喜欢
    • 2016-06-07
    • 2015-12-09
    • 2015-10-14
    • 1970-01-01
    • 2016-07-06
    • 2017-04-04
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    相关资源
    最近更新 更多