简体   繁体   English

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

[英]How to save the Facebook name with omniauth and devise

I'm trying to save the name of a Facebook user upon saving that user, but I don't seem to be able to. 我试图在保存该用户时保存Facebook用户的名字,但我似乎无法做到。 I have followed the guides on the devise github and the integration with Facebook works fine; 我遵循了设计github指南,与Facebook的整合工作正常; 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? 难道我做错了什么?

Seems like Auth hash schema has changed a lot: https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema 似乎Auth hash schema已经发生了很大变化: 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

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. 如果那不是您所需要的,我建议您检查access_token的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM