Facebook OAuth未在用户信息中返回电子邮件

我正在进行狂欢3.0安装(ROR)并尝试使用facebook oauth进行身份validation,但是在成功的oauth之后发回的字段不包含对我们的应用程序至关重要的电子邮件。 这是从facebook成功validation返回的。

#<OmniAuth::AuthHash credentials=# extra=#<OmniAuth::AuthHash raw_info=#> info=# provider="facebook" uid="101230990227589" 

正如你所看到的,我得到的只是用户名和他们的ID。 我的Facebook应用程序上是否有一些设置需要检查以便收回电子邮件? 或者我有什么不同的方式做Oauth? 我只是使用spree_social gem,它在内部完成这一切,所以我实际上并没有编写任何代码。

这是代码。 复制出gem,我只是添加了日志行,看看从Facebook回来的东西。

  def #{provider} authentication = Spree::UserAuthentication.find_by_provider_and_uid(auth_hash['provider'], auth_hash['uid']) if authentication.present? and authentication.try(:user).present? flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: auth_hash['provider']) sign_in_and_redirect :spree_user, authentication.user elsif spree_current_user spree_current_user.apply_omniauth(auth_hash) spree_current_user.save! flash[:notice] = I18n.t('devise.sessions.signed_in') redirect_back_or_default(account_url) else user = Spree::User.find_by_email(auth_hash['info']['email']) || Spree::User.new user.apply_omniauth(auth_hash) Rails.logger.debug("THE AUTO HASH") Rails.logger.debug(auth_hash.inspect) if user.save flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: auth_hash['provider']) sign_in_and_redirect :spree_user, user else session[:omniauth] = auth_hash.except('extra') flash[:notice] = Spree.t(:one_more_step, kind: auth_hash['provider'].capitalize) redirect_to new_spree_user_registration_url return end end if current_order user = spree_current_user || authentication.user current_order.associate_user!(user) session[:guest_token] = nil end end 

Facebook刚刚发布了最新的APIv2.4,默认情况下不会返回电子邮件,但我们需要明确指定要使用的字段。

介绍Graph API v2.4

现在,在最新的omniauth-facebook(可能是2.1.0)上,默认情况下指定“email”字段并且正常工作。

将默认info_fields修复为’name,email’#209

或者,你可以指定喜欢

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

可能您需要请求用户的电子邮件权限才能与您的FB应用程序共享电子邮件。

尝试添加此initilazer facebookstrategies.rb以请求facebookstrategies.rb的特定权限。

 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 

我有同样的问题,这就是我解决它的方式。

config/initializers/devise.rb添加以下代码:

 Devise.setup do |config| config.omniauth :facebook, your_app_id, your_app_secret, scope: 'public_profile,email', info_fields: 'email,name' end