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

rad*_*nyc 5 ruby-on-rails spree facebook-oauth

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

#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash expires=true expires_at=1442435073 token="CAAJa3dyBtY4BAJ2ZB3vrenNOFJKSMtvxYO09ZCJtEsmKNBs90q9nmUF4LIBr06xCizEAR3lwht3BwycLkVFdjlvkS1AUGpYODQHu25K0uO8XLDDPkTO0E9oPdIILsbTTOuIT7qcl8nJ6501z0dCXEi9hVNwPYqZBbGqiEhyoLyVgCNnDWdPRLBRF5xSovJdhjjCf6XC8ulJ4NnKBfM8"> extra=#<OmniAuth::AuthHash raw_info=#<OmniAuth::AuthHash id="101230990227589" name="David Alajbbjfdjgij Bowersstein">> info=#<OmniAuth::AuthHash::InfoHash image="http://graph.facebook.com/101230990227589/picture" name="David Alajbbjfdjgij Bowersstein"> provider="facebook" uid="101230990227589"
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我得到的只是用户名和他们的ID.在我的Facebook应用程序上是否有一些设置需要检查以便收回电子邮件?或者我有不同的方式做Oauth?我只是使用spree_social 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
Run Code Online (Sandbox Code Playgroud)

Tsu*_*oka 9

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';
Run Code Online (Sandbox Code Playgroud)