开发者

OmniAuth - Facebook login not supplying email in user_info

开发者 https://www.devze.com 2023-02-12 04:53 出处:网络
I\'m using OmniAuth, and after logging in via Facebook, I get my omniauth.auth key, which looks like this:

I'm using OmniAuth, and after logging in via Facebook, I get my omniauth.auth key, which looks like this:

user_info: 
  name: Tim Sullivan
  urls: 
    Facebook: http://www.facebook.com/...
    Website: 
  nickname: ...
  last_name: Sullivan
  first_name: Tim
uid: "123456789"
credentials: 
  token: [some token]
extra: 
  user_hash: 
    name: Tim Sullivan
    timezone: -5
    gender: male
    id: "123456789"
    last_name: Sullivan
    updated_time: 2010-12-30T00:52:39+0000
    verified: true
    locale: en_US
    link: http://www.facebook.com/...
    email: tim@myemailaddress.com
    first_name: Tim
provider: facebook

Now, according to the docs, the email should be in the user_info section, but it isn't. It is, however, in the extra/user_hash section. Since I'm stripping extra, it's not getting stored, so later on down the pipe I'm having problems. I could add it myself, but that doesn't explain why it's开发者_高级运维 not there in the first place.

Why isn't email being put into the user_info section? A bug? Undocumented change?


moved to

email = omniauth["extra"]["raw_info"]["email"]


The hash "info" contains all the information of the User:

email = omniauth["info"]["email"]


I think the doc is not up to date. I usually get it from the extra hash before removing it.

email = omniauth["extra"]["user_hash"]["email"]


While omniauth["info"] used to and should contain the information, I have noticed that facebook seems to be giving me errors with the email which is linked to a facebook bug/(feature?). So I get intermittent errors with this hash where the email is not present which breaks everything.

After much debugging I found that the safest way to not break my code is to call the FB API with Koala or just good ol REST and get the information needed for login if omniauth["info"] does not contain the information you need.


We are using omniauth with the FB JSDK and I couldn't get the email to come back because I had overlooked the fact that FB.login() requires a 'scope' opts.

 FB.login(function(response) {
   // handle the response
 }, {scope: 'email,user_likes'});

After adding the opts (even though the scope was set up on the server) everything was fixed.

https://developers.facebook.com/docs/reference/javascript/FB.login/v2.2#permissions


Since you're using Rails and not JavaScript (another person answered but for JS), you need to specifically ask for email to be returned from the info field hash as it isn't by default. You set this up in your config/initializers/omniauth.rb file like so:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, Rails.application.secrets.omniauth_provider_key, Rails.application.secrets.omniauth_provider_secret,
  :scope => 'email', :display => 'popup', :info_fields => 'name,email'
end

This info is kind of hidden at the very end of the Configuring section on the omniauth-facebook gem's GitHub readme.

0

精彩评论

暂无评论...
验证码 换一张
取 消