如何将 Facebook 用户数据转换为数据库中的记录?

How can I translate Facebook user data into records on my database?

我正在使用 omniauth-facebook gem 和 devise 在我的网站上设置 Facebook 登录。我正要将 Facebook 用户数据导入我的数据库,但我遇到了一些奇怪的问题。

这是我用来导入数据的方法:

#User.rb

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.email = auth.info.email.to_s
    user.password = Devise.friendly_token[0,20]
    user.username = auth.info.name
  end
end

我取回的记录有 user.username 等于 Facebook 电子邮件,user.email 为零。 auth.info.name(实际上是电子邮件地址)是我实际能够恢复的唯一 Facebook 用户数据。其他一切都归零。

有没有其他人遇到过这个?

为什么在 first_or_create 块中使用 user 而不是 member?创建代码片段时是否有错字?

无论如何,我认为问题出在数据库中存在的用户身上。在这种情况下,不会评估传递给 first_or_create 方法的块(产量仅适用于新记录),因此不会更新字段。

确保您在 omniauth-facebook 配置中指定了这些字段(或没有指定名称):https://github.com/mkdynamic/omniauth-facebook。默认为 returns 姓名和电子邮件。

你也应该看看很棒的RailsCasts about Facebook Authentication