简体   繁体   中英

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

I'm using Rails 4.2.7 with Omniauth 1.3.1. When someone logs into my app using Facebook, I'd like to record their birthday, however, I can't figure out how to extract it from Facebook's site when I got my auth object. I have this in my config/initializers/omniauth.rb file …

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'

but once my callback is returned to my application ..

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

The call dies because there is no birthday to extract …

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

What is printed out in “extra:” is

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/">>

even though I definitely have a birthday set up in my Facebook profile. What else do I need to do to pass/extract the birthday information?

According with Facebook's Login documentation , the permission for public_profile doesn't include birthday information, but only the following fields: id, name, first_name, last_name, age_range, link, gender, locale, picture, timezone, updated_time, verified .

The user_birthday permission will give you access to date and month of a person's birthday. They also state that it may or may not include the year.

Try adding user_birthday to your scope.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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