maeshimaの日記

メモ書きです

twitter_oauthを使ってみる

The Ruby Toolbox Searchtwitterで検索すると2位のtwitter_oauthのREADMEを見ながらtwitter で oauth 認証をしてみた。ダントツ1位の twitter gem もあったけどなんとなく。

moomerman's twitter_oauth at master - GitHub

unauthorized request example

oauthで認証してなくてもできることがいくつかあるらしい。

client = TwitterOAuth::Client.new  
puts client.show('twitter') #=> id:twitter のユーザ情報がhash形式で表示される
search = client.search('twitter')
search["results"] #=> "twitter" で検索したユーザの情報がhash形式で表示される

Authorized request example

oauthで認証する手順。下記のようにしてユーザがアクセスするurlを得る。consumer_keyとconsumer_secretはCreate cool applications! | dev.twitter.comでアプリを登録して取得する。

client = TwitterOAuth::Client.new(
    :consumer_key => 'YOUR_APP_CONSUMER_KEY',
    :consumer_secret => 'YOURA_APP_CONSUMER_SECRET'
)
request_token = client.request_token(:oauth_callback => oauth_confirm_url)
#:oauth_callback required for web apps, since oauth gem by default force PIN-based flow 
#( see http://groups.google.com/group/twitter-development-talk/browse_thread/thread/472500cfe9e7cdb9/848f834227d3e64d )

request_token.authorize_url
=> http://twitter.com/oauth/authorize?oauth_token=TOKEN

上記でoauth_callbackを指定しないとクライアントアプリのoauth認証と判断されるようで、oauth認証後に番号(oauth_verifier)が表示される。webアプリで使うならoauth_callbackの指定は必須。さらに、認証後のリダイレクト先で使用するために

  • request_token.token
  • request_token.secret

の二つを session 等に記録しておく必要がある。リダイレクト後は下記のようにしてアクセストークンを取得する。

access_token = client.authorize(
  request_token.token,
  request_token.secret,
  :oauth_verifier => params[:oauth_verifier]
)

client.authorized?
=> true

client.update('checking out the twitter_oauth library') # sends a twitter status update

一度認証したら、access_token と secret を保持しておけば二回目は下記のような感じで client を作れる

access_token = @user.access_token # assuming @user
client = TwitterOAuth::Client.new(
    :consumer_key => 'YOUR_CONSUMER_KEY',
    :consumer_secret => 'YOUR-CONSUMER-SECRET',
    :token => access_token.token, 
    :secret => access_token.secret
)

client.authorized?
=> true

認証通った後使うメソッドは下記を参照。

Documentation for moomerman/twitter_oauth (master)

TwitterOAuth::Client#info

client.info # => {"name"=>"willnet", "profile_sidebar_border_color"=>"87bc44", 
"profile_background_tile"=>false, "profile_sidebar_fill_color"=>"e0ff92", "location"=>"tokyo", 
"profile_image_url"=>"http://a0.twimg.com/profile_images/838092920/CIMG6387_normal.jpg", "created_at"=>"Sat Jun 23 02:41:24 +0000 2007", 
"profile_link_color"=>"0000ff", "follow_request_sent"=>false, 
"contributors_enabled"=>false, "url"=>"http://d.hatena.ne.jp/willnet/", "favourites_count"=>66, "utc_offset"=>32400, 
"id"=>7028812, "listed_count"=>33, 
"profile_use_background_image"=>true, "followers_count"=>757, 
"protected"=>false, "lang"=>"en", "profile_text_color"=>"000000", 
"profile_background_color"=>"9ae4e8", "geo_enabled"=>false, 
"verified"=>false, "notifications"=>false, "time_zone"=>"Tokyo", 
"description"=>"東京在住。ITベンチ ャー企業でWEB開発してます。おもしろいWEBサービスをどしどし作っていく予定\343\200\202", 
"statuses_count"=>6633, 
"profile_background_image_url"=>"http://s.twimg.com/a/1283564528/images/themes/theme1/bg.png", "status"=>{"coordinates"=>nil, 
"truncated"=>false, "created_at"=>"Wed Sep 08 05:12:27 +0000 2010", 
"favorited"=>false, 
"text"=>"買った RT @monoooki: iPhoneアプリ ひらがなゲーム かなぶん 期間限定セールは今日(9/8)までですー。350円→115円になってます。 http://bit.ly/kanabun_jp 解説ブログもどうぞ〜 http://htn.to/cY3YmS", "contributors"=>nil, "id"=>23887302219, "retweet_count"=>nil, 
"geo"=>nil, "retweeted"=>false, "in_reply_to_user_id"=>nil, 
"place"=>nil, "in_reply_to_screen_name"=>nil, "source"=>"<a href=\"http://www.echofon.com/\" rel=\"nofollow\">Echofon</a>", 
"in_reply_to_status_id"=>nil}, "friends_count"=>751, "following"=>false, "screen_name"=>"netwillnet", 
"show_all_inline_media"=>false}