Facebook OAuth is not returning email in user info

独自空忆成欢 提交于 2019-12-05 08:44:25

Facebook just released latest APIv2.4 that does not return email by default but we need to explicitly specify what fields to use.

Introducing Graph API v2.4

Now, on the very latest omniauth-facebook(possibly 2.1.0), "email" fields are specified by default and just works.

Fix default info_fields to 'name,email' #209

Or, you can just specify like

options['info_fields']='id,email,gender,link,locale,name,timezone,updated_time,verified';

Possibly you need to request the email permission from user to share email to your FB App.

Try to add this initilazer facebookstrategies.rb to request the specific permission to facebook.

module OmniAuth
  module Strategies
    class Facebook < OAuth2

      MOBILE_USER_AGENTS =  'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' +
                          'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' +
                          'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' +
                          'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
                          'webos|amoi|novarra|cdm|alcatel|pocket|ipad|iphone|mobileexplorer|' +
                          'mobile'
      def request_phase
        options[:scope] ||= "email,offline_access,user_birthday,public_profile"
        options[:display] = mobile_request? ? 'touch' : 'page'
       super
      end

      def mobile_request?
        ua = Rack::Request.new(@env).user_agent.to_s
        ua.downcase =~ Regexp.new(MOBILE_USER_AGENTS)
      end
    end
  end
end
Христијан С.

I had the same issue and this is how I solved it.

In config/initializers/devise.rb add following code:

Devise.setup do |config|
  config.omniauth :facebook, your_app_id, your_app_secret, scope: 'public_profile,email', info_fields: 'email,name'
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!