【问题标题】:How to save the Facebook name with omniauth and devise如何使用omniauth 保存Facebook 名称并设计
【发布时间】:2011-11-13 07:11:43
【问题描述】:

我正在尝试在保存 Facebook 用户时保存该用户的姓名,但我似乎无法做到。我关注了guides on the 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

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

【问题讨论】:

    标签: ruby-on-rails devise omniauth


    【解决方案1】:

    似乎 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
    

    【讨论】:

      【解决方案2】:

      用户名存储在:

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

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

      【讨论】:

      • 不幸的是,没有。无法从该方法中访问请求,我在this stack overflow post 中找到了一个使用'data [“name”]'的示例,并查看Facebooks'文档,“name”应该在“[”中额外的"]["user_hash"]'
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      相关资源
      最近更新 更多