問題
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.
デフォルトは、上記サンプルのように、nameとidとimageしかないです。
対応方法
-
omniauth-facebook側の対応を待つを使う- 試しにPullRequest #209を出してみましたが、一応マージされました!!
英語が下手なので説明が理解されるかどうか、マージしてくれるかどうかはあんまり自信がない。。。 - 一応上記の修正で今までどおりに
emailの値を取得出来ました。 - PullReuqstはマージされましたが、gemの新しいバージョンまだリリースされてないので、Gemfileでgithub直接指定する必要がある ⇒
gem 'omniauth-facebook', github: 'mkdynamic/omniauth-facebook'
- 試しにPullRequest #209を出してみましたが、一応マージされました!!
-
omniauth-facebookのinfo_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