如何使用omniauth和devise保存Facebook名称

发布于 2024-12-03 19:07:04 字数 631 浏览 5 评论 0原文

我试图在保存 Facebook 用户时保存该用户的姓名,但我似乎无法做到这一点。我已遵循 devise github 上的指南,并且与 Facebook 的集成有效美好的;用户的电子邮件按预期保存。但是,我不知道如何保存用户名。现在,我这样做:

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token['extra']['user_hash']
    if user = User.find_by_email(data["email"])
      user
    else # Create a user with a stub password.
      User.create(:email => data["email"], :name => data["name"], :password => Devise.friendly_token[0,20]) 
    end
  end

但这不起作用。我做错了什么吗?

I'm trying to save the name of a Facebook user upon saving that user, but I don't seem to be able to. I have followed the guides on the devise github and the integration with Facebook works fine; the users email is saved as is to be expected. However, I can't figure out how to save the users name. Right now, I do this:

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token['extra']['user_hash']
    if user = User.find_by_email(data["email"])
      user
    else # Create a user with a stub password.
      User.create(:email => data["email"], :name => data["name"], :password => Devise.friendly_token[0,20]) 
    end
  end

but that doesn't work. Am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-12-10 19:07:04

似乎 Auth 哈希模式发生了很大变化:
https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
  data = access_token.extra.raw_info
  if user = User.where(:email => data.email).first
    user
  else
    # Create a user with a stub password. 
    user = User.create!(:username => data.username ? data.username : data.nickname , :email => data.email, :password => Devise.friendly_token[0,20])
  end
end

Seems like Auth hash schema has changed a lot:
https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
  data = access_token.extra.raw_info
  if user = User.where(:email => data.email).first
    user
  else
    # Create a user with a stub password. 
    user = User.create!(:username => data.username ? data.username : data.nickname , :email => data.email, :password => Devise.friendly_token[0,20])
  end
end
恰似旧人归 2024-12-10 19:07:04

用户名存储在:

auth = request.env['omniauth.auth']  #I think this is what your access_token variable equates to.
auth['user_info']['name']

如果这不是您需要的,我建议您检查 access_token 的内容。

The users name is stored in:

auth = request.env['omniauth.auth']  #I think this is what your access_token variable equates to.
auth['user_info']['name']

If that is not what you need, I suggest you inspect the contents of the access_token.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文