From 4397254a1dae8c2d0e42285de44c59fb95f22ed9 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sat, 28 Mar 2009 20:44:23 -0400 Subject: [PATCH] Added basic twitter oauth support and an example on how to use it. Very rough right now and no integration with other methods. --- Rakefile | 2 +- examples/oauth.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++ lib/twitter.rb | 6 +++++ lib/twitter/oauth.rb | 32 +++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 examples/oauth.rb create mode 100644 lib/twitter/oauth.rb diff --git a/Rakefile b/Rakefile index 0df62f26c..cea28a0e4 100644 --- a/Rakefile +++ b/Rakefile @@ -12,7 +12,7 @@ Echoe.new(ProjectName, Twitter::Version) do |p| p.url = "http://#{ProjectName}.rubyforge.org" p.author = "John Nunemaker" p.email = "nunemaker@gmail.com" - p.extra_deps = [['hpricot', '>= 0.6'], ['activesupport', '>= 2.1'], ['httparty', '>= 0.2.4']] + p.extra_deps = [['oauth', '>= 0.3.2'], ['hpricot', '>= 0.6'], ['activesupport', '>= 2.1'], ['httparty', '>= 0.2.4']] p.need_tar_gz = false p.docs_host = WebsitePath end diff --git a/examples/oauth.rb b/examples/oauth.rb new file mode 100644 index 000000000..02433d2c4 --- /dev/null +++ b/examples/oauth.rb @@ -0,0 +1,60 @@ +require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter') +require 'pp' + +class ConfigStore + attr_reader :file + + def initialize(file) + @file = file + end + + def load + @config ||= YAML::load(open(file)) + self + end + + def [](key) + load + @config[key] + end + + def []=(key, value) + @config[key] = value + end + + def update(c={}) + @config.merge!(c) + save + end + + def save + File.open(file, 'w') { |f| f.write(YAML.dump(@config)) } + end +end + +config = ConfigStore.new("#{ENV['HOME']}/.twitter") +oauth = Twitter::OAuth.new(config['token'], config['secret']) + +if config['atoken'] && config['asecret'] + oauth.authorize_from_access(config['atoken'], config['asecret']) + puts oauth.access_token.get("/statuses/friends_timeline.json") + +elsif config['rtoken'] && config['rsecret'] + oauth.authorize_from_request(config['rtoken'], config['rsecret']) + puts oauth.access_token.get("/statuses/friends_timeline.json") + + config.update({ + 'atoken' => oauth.access_token.token, + 'asecret' => oauth.access_token.secret, + 'rtoken' => nil, + 'rsecret' => nil, + }) +else + config.update({ + 'rtoken' => oauth.request_token.token, + 'rsecret' => oauth.request_token.secret, + }) + + # authorize in browser + %x(open #{oauth.request_token.authorize_url}) +end \ No newline at end of file diff --git a/lib/twitter.rb b/lib/twitter.rb index f2b0a2ef1..90f948a60 100644 --- a/lib/twitter.rb +++ b/lib/twitter.rb @@ -4,11 +4,17 @@ require 'yaml' require 'time' require 'rubygems' + +gem 'hpricot' require 'hpricot' +gem 'oauth' +require 'oauth' + $:.unshift(File.dirname(__FILE__)) require 'twitter/version' require 'twitter/easy_class_maker' +require 'twitter/oauth' require 'twitter/base' require 'twitter/user' require 'twitter/search' diff --git a/lib/twitter/oauth.rb b/lib/twitter/oauth.rb new file mode 100644 index 000000000..82af0255e --- /dev/null +++ b/lib/twitter/oauth.rb @@ -0,0 +1,32 @@ +module Twitter + class OAuth + attr_reader :token, :secret + + def initialize(ctoken, csecret) + @ctoken, @csecret = ctoken, csecret + end + + def request_token + @request_token ||= consumer.get_request_token + end + + def authorize_from_request(rtoken, rsecret) + request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret) + access_token = request_token.get_access_token + @atoken, @asecret = access_token.token, access_token.secret + end + + def access_token + @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret) + end + + def authorize_from_access(atoken, asecret) + @atoken, @asecret = atoken, asecret + end + + private + def consumer + @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => 'http://twitter.com'}) + end + end +end \ No newline at end of file