简体   繁体   中英

omniauth-facebook cannnot get email address

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?

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.

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