【问题标题】:Laravel Socialite get more information about the user - user_locationLaravel Socialite 获取有关用户的更多信息 - user_location
【发布时间】:2016-10-14 06:11:37
【问题描述】:

当用户登录时,我想要用户 facebook 的其他字段,例如 user_location

现在我知道我需要提交一个应用程序进行审核才能获得批准以获取用户位置。 Facebook也已经批准了。

现在我正在尝试使用以下代码获取:

$this->socialite->driver($provider)->scopes(['user_location', 'user_hometown'])->redirect()

但是当我转储这个时:

$this->socialite->driver($provider)->user();

它没有在用户数据数组中为我提供 user_location。但是当我第一次尝试登录时,facebook 要求它的许可。

这是我得到的数据:

User {#330 ▼
  +token: "xxxxxxxxxx"
  +refreshToken: null
  +expiresIn: "xxxxx"
  +id: "xxxxxx"
  +nickname: null
  +name: "xxxxxx"
  +email: "xxxxxx@xx.com"
  +avatar: "xxxxxx"
  +user: array:5 [▶]
  +"avatar_original": "xxxxxxxx"
}

所以我可以知道,我做错了什么,以及如何获取用户位置。

提前谢谢大家

【问题讨论】:

    标签: facebook laravel laravel-socialite


    【解决方案1】:

    您可以在重定向方法中使用 fields() 方法来设置其他字段并稍后在您的应用程序中使用它们。

    public function redirectToFacebook()
    {
        return $this->socialite->driver($provider)->fields([
            'first_name', 'last_name', 'email', 'gender','location'
        ])->scopes([
            'user_location'
        ])->redirect();
    }
    

    然后通过以下方式访问它们:

    public function handleFacebookCallback()
    {
        $providerUser = $this->socialite->driver($provider)->fields([
            'first_name', 'last_name', 'email', 'gender', 'birthday','location'
        ])->user();
    
        dd($providerUser);
    }
    

    请注意,fields() 将覆盖默认值,因此在上面的示例中,您不会获得用户的“已验证”和“链接”属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 2020-06-30
      • 2021-04-15
      • 2018-11-30
      • 2016-02-23
      • 2020-04-24
      相关资源
      最近更新 更多