Devise + Omniauth-Facebook获得用户的电话#

时间:2014-06-27 20:27:35

标签: ruby-on-rails facebook authentication devise omniauth

我在我的Rails 3.2应用中使用Devise进行身份验证,但在配置omniauth-facebook以获取新用户的电话号码时遇到问题。

首先:我甚至不确定是否可以获得电话号码,所以如果情况确实如此,有人确切知道,我会很高兴只是为了确认。

来自https://github.com/mkdynamic/omniauth-facebook&#34;电话&#34;默认情况下是FB auth哈希的一部分,尽管一般的Omniauth模式确实有.info.phone(当然不是必需的)。所以我的第一个想法是它是一个FB权限问题。我不确定要使用哪些权限,因为Permissions with Facebook Login页面没有说明在哪里可以找到电话价值(也许这意味着它不可能?)。< / p>

我有phone作为User模型的必需属性,所以当我尝试通过FB获取它时,新对象永远不会存在。无需查找电话号码即可正常使用。

我的配置文件:

# /config/initializers/devise.rb

config.omniauth :facebook, 'FACEBOOK_APP_ID', 'FACEBOOK_APP_SECRET', scope: 'email,public_profile', display: 'page'

在我的用户模型中:

# user.rb

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.first_name = auth.info.first_name
      user.last_name = auth.info.last_name
      user.phone = auth.extra.raw_info.phone  # have also tried auth.info.phone
    end
  end

提前感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:5)

目前无法从Facebook获取用户的电话号码。

Facebook's Graph API reference列出了您可以通过API访问的所有用户信息。用户的电话号码不在列表中。

此外,要检查auth哈希的内容,请在身份验证/会话/回调控制器操作的开头添加以下代码行:

render :text => "<pre>" + env["omniauth.auth"].to_yaml and return

您会在phoneauth.extra.raw_info看到没有auth.info字段。