Home > Ruby > RubyでTwitter

RubyでTwitter

  • 2010-05-03 (月) 19:41
  • Ruby

最初に書いたoauth.rbでトラブルに遭って困った。

#!/usr/local/bin/ruby -Ku
require 'rubygems'
require 'oauth'
require 'twitter'

すると

./oauth.rb:13: uninitialized constant OAuth (NameError)
	from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
	from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
	from oauth.rb:3

となってしまう。原因は簡単で、ファイル名がoauth.rbだったからである。requireはカレントディレクトリを優先して読みに行くので、自分自身をrequireしていた。解決策もシンプルで別の名前にすればいい。

OAuth認証でTwitterを使う

まず

[root@PC twitter]# gem install twitter
When you HTTParty, you must party hard!
Building native extensions.  This could take a while...
Successfully installed oauth-0.4.0
Successfully installed hashie-0.2.0
Successfully installed crack-0.1.6
Successfully installed httparty-0.5.2
Successfully installed yajl-ruby-0.7.6
Successfully installed twitter-0.9.5
6 gems installed
Installing ri documentation for oauth-0.4.0...
Installing ri documentation for hashie-0.2.0...
Installing ri documentation for crack-0.1.6...
Installing ri documentation for httparty-0.5.2...
Installing ri documentation for yajl-ruby-0.7.6...
Installing ri documentation for twitter-0.9.5...
Installing RDoc documentation for oauth-0.4.0...
Installing RDoc documentation for hashie-0.2.0...
Installing RDoc documentation for crack-0.1.6...
Installing RDoc documentation for httparty-0.5.2...
Installing RDoc documentation for yajl-ruby-0.7.6...
Installing RDoc documentation for twitter-0.9.5...

一緒にoauth-0.4.0もインストールされる。

アプリケーションを登録する

  1. Twitterのアカウントがなければ取得する
  2. Twitterにログインする
  3. ログインした状態でWebブラウザで「Twitter / アプリケーション」にアクセスする
  4. 「新しいアプリケーションを追加」をする
  5. 必要事項を記入する

アプリ名などは自由に記入すればいい。わかりにくいのは「あなたの招待状」は「送信」を選ぶとよい。そうすると出てくる「Default Access type」は「Read & Write」にしておいた方がいいだろう。

必要事項を記入するとConsumer keyとConsumer secretなどが表示されるので記録しておく。

Access token, Access token secretを取得する

#!/usr/local/bin/ruby -Ku
require 'rubygems'
require 'oauth'

# http://d.hatena.ne.jp/shibason/20090802/1249204953を参考にしました

#OAuth
CONSUMER_KEY = "きー"
CONSUMER_SECRET = "しーくれっと"

consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
request_token = consumer.get_request_token

puts "Access this URL and approve => #{request_token.authorize_url}"
print "Input OAuth Verifier: "
oauth_verifier = gets.chomp.strip

access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier)

puts "Access token: #{access_token.token}"
puts "Access token secret: #{access_token.secret}"

これを適切な名前を付けて保存する。oauth.rbにしたらひどいトラブルで苦労したので気をつける。実行すると

Access this URL and approve => http://twitter.com/oauth/authorize?oauth_token=とーくん
Input OAuth Verifier: 

と出てくるので、URLにアクセスして出てきた数値を入力すると、Access tokenとAccess token secretが得られる。これを記録しておく。

Twitterに何か書いてみる

#!/usr/local/bin/ruby -Ku
require 'rubygems'
require 'oauth'
require 'twitter'

CONSUMER_KEY = "きー"
CONSUMER_SECRET = "しーくれっと"
ACCESS_TOKEN = "とーくん"
ACCESS_TOKEN_SECRET = "とーくんしーくれっと"

consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
oauth = Twitter::OAuth.new(CONSUMER_KEY, CONSUMER_SECRET)
oauth.authorize_from_access(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitter_client = Twitter::Base.new(oauth)
twitter_client.update("こけこっこ")

実行すると「こけこっこ」とつぶやきが投稿される。

Comments:0

Comment Form
Remember personal info

Trackback+Pingback:0

TrackBack URL for this entry
http://blog.neoneet.jp/2010/05/03/ruby%e3%81%a7twitter/trackback/
Listed below are links to weblogs that reference
RubyでTwitter from 週刊(月刊?)プレカリアート

Home > Ruby > RubyでTwitter

Search
Feeds
Meta

Page Top