maeshimaの日記

メモ書きです

facebook_oauth

twitter_oauthと同じ作者が作ったfacebook用のgem。

moomerman's facebook_oauth at master - GitHub

Authorized request example

twitter の oauth と同じような感じ。ただなぜか最初のうち 401 になった。アプリ設定のweb site→サイトURLを入れたらうまくいったみたい。

client = FacebookOAuth::Client.new(
    :application_id => 'YOUR_APPLICATION_ID',
    :application_secret => 'YOUR_APP_SECRET_KEY',
    :callback => 'http://example.com/facebook/callback'
)

client.authorize_url
=> "https://graph.facebook.com/oauth/authorize?scope=SCOPE&client_id=ID&type=web_server&redirect_uri=CALLBACK"

また、client.authorize_url(:scope => "publish_stream")

のようにすることでurlに含まれるSCOPEの部分を修正できる。SCOPEとはアプリが利用を許可される機能のこと。複数指定する場合はカンマで区切る。何が指定できるかとその意味はExtended Permissions - Facebook開発者を参照。デフォルトだとoffline_accessになって、これはユーザが出来ることは全部できる&ずっと使えるみたいな権限らしい。


facebook上の認証が終わるとコールバックURLにcodeというクエリつきでリダイレクトされる。

access_token = client.authorize(:code => params[:code])
client.me.info # returns your user information

access_token.token をDBに入れておく。そうすると次回以降の認証プロセスがいらなくなる。

access_token = @user.access_token # assuming @user
client = FacebookOAuth::Client.new(
    :application_id => 'YOUR_APPLICATION_ID',
    :application_secret => 'YOUR_APP_SECRET_KEY',
    :token => access_token
)
client.me.info # returns your user information

oauth認証終わってからの操作

第一引数に :create を指定するとリソースの作成が出来るみたい。

client.me.feed(:create, :message => 'Testing writing to your wall...')

残りはリファレンス見て理解しろとのこと。

Graph API reference - Facebook開発者

client.me.info

client.me.info 
#=> {"name"=>"Shinichi Maeshima",
"timezone"=>9,
"gender"=>"\346\200\247",
"id"=>"1033476295",
"last_name"=>"Maeshima",
"updated_time"=>"2010-06-17T11:44:43+0000",
"locale"=>"en_US",
"link"=>"http://www.facebook.com/shinichi.maeshima",
"education"=>[{"school"=>{"name"=>"Waseda University", "id"=>105686912798519},
"year"=>{"name"=>"2005", "id"=>102278836480694}}],
"work"=>[{"start_date"=>"0000-00", "employer"=>{"name"=>"株式会社ハートレイル\343\202\272", "id"=>113770655306283}}],
"first_name"=>"Shinichi"}