LoginSignup
18
19

More than 5 years have passed since last update.

omniauth-facebook+GraphAPI ver 2.4でemailフィールドが取得されない件の対応

Last updated at Posted at 2015-07-12

問題

omniauth-facebookでFacebookログイン機能を実装しようとしたら、何故かemailフィールドが取得されません。
omniauth.authの値はこんな感じです。

"omniauth.auth":{
  "provider":"facebook",
  "uid":"xxxxxx",
  "info":{
    "name":"xxx",
    "image":"http://graph.facebook.com/....."
  },
  "credentials":{
    "token":"...",
    "expires_at":1441880921,
    "expires":true
  },
  "extra":{
    "raw_info": {
      "name":"xxx",
      "id":"xxxxxxxxx"
    }
  }
}

原因

graph-api ver 2.4 で、下記の変更があったようです。

Fewer default fields for faster performance: To help improve performance on mobile network connections, we've reduced the number of fields that the API returns by default. You should now use the ?fields=field1,field2 syntax to declare all the fields you want the API to return.

デフォルトは、上記サンプルのように、nameidimageしかないです。

対応方法

  • omniauth-facebook側の対応を待つ を使う
    • 試しにPullRequest #209を出してみましたが、一応マージされました!! 英語が下手なので説明が理解されるかどうか、マージしてくれるかどうかはあんまり自信がない。。。
    • 一応上記の修正で今までどおりにemailの値を取得出来ました。
    • PullReuqstはマージされましたが、gemの新しいバージョンまだリリースされてないので、Gemfileでgithub直接指定する必要がある ⇒ gem 'omniauth-facebook', github: 'mkdynamic/omniauth-facebook'
  • omniauth-facebookinfo_fieldsオプションで対応する
    • 普段はあんまり使われてないオプションかも知れません ⇒ info_fields
    • 注意: emailだけに設定すると、nameフィールドの値もメールアドレスになるため、name,emailに設定する必要があります。
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
           :scope => 'email,user_birthday,read_stream', :display => 'popup',
           :info_fields => 'name, email' # ←追加する部分
end
18
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19