如何在 RoR 中使用 omniauth 提取 Facebook 生日信息?

How do I use omniauth in RoR to extract Facebook birthday information?

我正在使用 Rails 4.2.7 和 Omniauth 1.3.1。当有人使用 Facebook 登录我的应用程序时,我想记录他们的生日,但是,当我得到我的 auth 对象时,我无法弄清楚如何从 Facebook 的网站中提取它。我的 config/initializers/omniauth.rb 文件中有这个……

Rails.application.config.middleware.use OmniAuth::Builder do
    …   

  provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
           scope: 'public_profile', info_fields: 'id,email,link,birthday,first_name,last_name'

但是一旦我的回调返回到我的应用程序..

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])

通话中断,因为没有生日可提取……

def self.from_omniauth(auth)
  puts "extra: #{auth.extra}" 
  bday = Date.strptime(auth.extra.raw_info.birthday,'%m/%d/%Y')

“extra:”中打印出来的是

extra: #<OmniAuth::AuthHash raw_info=#<OmniAuth::AuthHash first_name=“MyName” id="1777736954079999” last_name=“LastName” link="https://www.facebook.com/app_scoped_user_id/1777736954079999/">>

虽然我肯定在我的 Facebook 个人资料中设置了生日。 pass/extract生日信息还需要做什么?

根据Facebook的登录documentationpublic_profile的权限不包含生日信息,只包含以下字段:id, name, first_name, last_name, age_range, link, gender, locale, picture, timezone, updated_time, verified.

user_birthday 权限将使您能够访问某人的生日日期和月份。他们还声明它可能包括也可能不包括年份。

尝试将 user_birthday 添加到您的范围。

provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
       scope: 'public_profile,user_birthday', info_fields: 'id,email,link,birthday,first_name,last_name'