2012年11月28日水曜日

Rails3.2.8 facebookのomniauthで画像(サムネイル),誕生日を取得

以下のリストにあるものが取得できるもの。

でfacebookのサムネイル画像を取得したい場合、
models/user.rb
user.image = auth.info.image
でDB内に保存させ、
ビューで
<%= image_tag(user.image)してあげれば、とってこれる。


 provider: facebook
 uid: "xxxx"
 info: !map:OmniAuth::AuthHash::InfoHash
   nickname: xxxx
   email: xxxx
   name: xxxx
   first_name: xxxx
   last_name: xxxx
   image: http://graph.facebook.com/xxxx
   urls: !map:Hashie::Mash
     Facebook: http://www.facebook.com/xxxx
   verified: true
 credentials: !map:Hashie::Mash
   token: xxxx
   expires_at: xxxx
   expires: true
 extra: !map:Hashie::Mash
   raw_info: !map:Hashie::Mash
     id: "xxxx"
     name: xxxx
     first_name: xxxx
     last_name: xxxx
     link: http://www.facebook.com/xxxx
     username: xxxx
     gender: xxxx
     email: xxxx
     timezone: 9
     locale: ja_JP
     verified: true
     updated_time: xxxx
誕生日を取得したい場合、
これとは別途permissionが必要になる。

その場合、つくったconfig/initializer/omniauth.rbの中に

 
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, "id", "pass", {scope: "user_birthday"}
end
とscopeを追加。
model/omniuser.rb
if auth.extra.raw_info.birthday
        omniuser.birthday = Date.strptime(auth.extra.raw_info.birthday, '%m/%d/%Y')
end
でもってくることができる。
参考:公式のpermissionのところを参照してください
https://developers.facebook.com/docs/reference/login/#permissions