omniauth-facebook cannnot get email address

谁说我不能喝 提交于 2020-01-01 02:40:34

问题


I created new Rails App and install Devise and omniauth-facebook gem.

And setting my Facebook App, as testing environment.

So, I logged in via facebook and signed up my new Rails app, but request.env did not contain email address.info.

this is returned request.env['omniauth.auth']

{
   "provider" => "facebook",
   "uid" => "xxxxxxxxxxxx",
   "info" => {
         "name" => "xxxxxxx",
        "image" => "http://graph.facebook.com/xxx/picture"
    },
    "credentials" => {
             "token" => "tokenstring",
        "expires_at" => xxxxxxxxx,
           "expires" => true
    },
          "extra" => {
        "raw_info" => {
            "name" => "xxx xxxx",
              "id" => "xxxxxxxxx"
        }
    }
}

it racks request.env['omniauth.auth']['info']['email']

How can I get email address from facebook via oauth? Please anyone help me.

Rails ver is 4.2.3 Ruby ver is 2.2.2p95

This is Gem Versions

 omniauth (1.2.2)
 omniauth-facebook (2.0.1)
 devise (3.5.1)

config/initializers/devise.rb

  config.omniauth :facebook, 'appId', 'appSeacret', scope: 'email,public_profile'

app/controllers/omniauth_callbacks_controller.rb

  def all_provider
    user = User.from_omniauth(request.env['omniauth.auth'])
    if user.persisted?
      sign_in_and_redirect user
    else
      session['devise.user_attributes'] = user.attributes
      redirect_to new_user_registration_url
    end
  end

  alias_method :facebook, :all_provider

app/models/user.rb

def from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.email = auth.info.email
  end
end

Add at 2015/7/11

I retried same code with my old test facebook application, and COULD get full public_profile and email.

Are any restrictions added for new facebook app?? someone knows?


回答1:


I found the way to get email address myself.

https://developers.facebook.com/docs/apps/changelog#v2_4

This doc says that

Declarative Fields To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.

so, it need to write in config/initializers/devise.rb

  config.omniauth :facebook, 'app_id', 'app_secret',  scope: 'email', info_fields: 'email'
  • scope: 'email' is default

not only scope, but also info_fields.



来源:https://stackoverflow.com/questions/31337829/omniauth-facebook-cannnot-get-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!