Permalink
Browse files

* added Twitter and FriendFeed sample. see #227.

  • Loading branch information...
nahi
nahi committed Oct 29, 2009
1 parent e4718e5 commit bc6fc4fe74011b4864514ccd0605478c4bfda647
Showing with 154 additions and 17 deletions.
  1. +15 −3 lib/httpclient/auth.rb
  2. +19 −14 lib/oauthclient.rb
  3. +59 −0 sample/oauth_friendfeed.rb
  4. +61 −0 sample/oauth_twitter.rb
View
@@ -541,35 +541,45 @@ class OAuth
class Config
include HTTPClient::Util
+ attr_accessor :http_method
attr_accessor :realm
attr_accessor :consumer_key
attr_accessor :consumer_secret
attr_accessor :token
attr_accessor :secret
attr_accessor :signature_method
attr_accessor :version
+ attr_accessor :callback
+ attr_accessor :verifier
attr_reader :signature_handler
attr_accessor :debug_timestamp
attr_accessor :debug_nonce
def initialize(*args)
- @realm,
+ @http_method,
+ @realm,
@consumer_key,
@consumer_secret,
@token,
@secret,
@signature_method,
- @version =
+ @version,
+ @callback,
+ @verifier =
keyword_argument(args,
+ :http_method,
:realm,
:consumer_key,
:consumer_secret,
:token,
:secret,
:signature_method,
- :version
+ :version,
+ :callback,
+ :verifier
)
+ @http_method ||= :post
@signature_handler = {}
end
end
@@ -664,6 +674,8 @@ def calc_cred(req, config)
header['oauth_timestamp'] = config.debug_timestamp || Time.now.to_i.to_s
header['oauth_nonce'] = config.debug_nonce || generate_nonce()
header['oauth_version'] = config.version if config.version
+ header['oauth_callback'] = config.callback if config.callback
+ header['oauth_verifier'] = config.verifier if config.verifier
signature = sign(config, header, req)
header['oauth_signature'] = signature
str = header.map { |k, v| encode_header(k, v) }.join(', ')
View
@@ -29,27 +29,23 @@ def initialize(*arg)
self.www_auth.oauth.challenge(nil)
end
- def get_request_token(uri)
+ def get_request_token(uri, callback = nil, param = nil)
oauth_config.token = nil
oauth_config.secret = nil
- res = get(uri)
- if res.status == 200
- res.oauth_params = get_oauth_response(res)
- end
+ oauth_config.callback = callback
+ oauth_config.verifier = nil
+ res = request(oauth_config.http_method, uri, param)
+ filter_response(res)
res
end
- def get_access_token(uri, request_token, request_token_secret)
+ def get_access_token(uri, request_token, request_token_secret, verifier = nil)
oauth_config.token = request_token
oauth_config.secret = request_token_secret
- res = get(uri)
- if res.status == 200
- res.oauth_params = h = get_oauth_response(res)
- if h
- oauth_config.token = h['oauth_token']
- oauth_config.secret = h['oauth_token_secret']
- end
- end
+ oauth_config.callback = nil
+ oauth_config.verifier = verifier
+ res = request(oauth_config.http_method, uri)
+ filter_response(res)
res
end
@@ -59,6 +55,15 @@ def unescape(escaped)
::HTTPClient::HTTP::Message.unescape(escaped)
end
+ def filter_response(res)
+ if res.status == 200
+ if res.oauth_params = get_oauth_response(res)
+ oauth_config.token = res.oauth_params['oauth_token']
+ oauth_config.secret = res.oauth_params['oauth_token_secret']
+ end
+ end
+ end
+
def get_oauth_response(res)
enc = res.header['content-encoding']
body = nil
View
@@ -0,0 +1,59 @@
+require 'oauthclient'
+
+# Get your own consumer token from http://friendfeed.com/api/applications
+consumer_key = 'EDIT HERE'
+consumer_secret = 'EDIT HERE'
+
+request_token_url = 'https://friendfeed.com/account/oauth/request_token'
+oob_authorize_url = 'https://friendfeed.com/account/oauth/authorize'
+access_token_url = 'https://friendfeed.com/account/oauth/access_token'
+
+STDOUT.sync = true
+
+# create OAuth client.
+client = OAuthClient.new
+client.oauth_config.consumer_key = consumer_key
+client.oauth_config.consumer_secret = consumer_secret
+client.oauth_config.signature_method = 'HMAC-SHA1'
+client.oauth_config.http_method = :get # FriendFeed does not allow :post
+client.debug_dev = STDERR if $DEBUG
+
+# Get request token.
+res = client.get_request_token(request_token_url)
+p res.status
+p res.oauth_params
+p res.content
+p client.oauth_config
+token = res.oauth_params['oauth_token']
+secret = res.oauth_params['oauth_token_secret']
+raise if token.nil? or secret.nil?
+
+# You need to confirm authorization out of band.
+puts
+puts "Go here and do confirm: #{oob_authorize_url}?oauth_token=#{token}"
+puts "Hit [enter] to go"
+gets
+
+# Get access token.
+# FYI: You may need to re-construct OAuthClient instance here.
+# In normal web app flow, getting access token and getting request token
+# must be done in different HTTP requests.
+# client = OAuthClient.new
+# client.oauth_config.consumer_key = consumer_key
+# client.oauth_config.consumer_secret = consumer_secret
+# client.oauth_config.signature_method = 'HMAC-SHA1'
+# client.oauth_config.http_method = :get # Twitter does not allow :post
+res = client.get_access_token(access_token_url, token, secret)
+p res.status
+p res.oauth_params
+p res.content
+p client.oauth_config
+username = res.oauth_params['username']
+
+puts
+puts "Access token usage example"
+puts "Hit [enter] to go"
+gets
+
+# Access to a protected resource. (user profile)
+puts client.get("http://friendfeed-api.com/v2/feedinfo/#{username}?format=json")
View
@@ -0,0 +1,61 @@
+require 'oauthclient'
+
+# Get your own consumer token from http://twitter.com/apps
+consumer_key = 'EDIT HERE'
+consumer_secret = 'EDIT HERE'
+
+callback = ARGV.shift # can be nil for OAuth 1.0. (not 1.0a)
+request_token_url = 'http://twitter.com/oauth/request_token'
+oob_authorize_url = 'http://twitter.com/oauth/authorize'
+access_token_url = 'http://twitter.com/oauth/access_token'
+
+STDOUT.sync = true
+
+# create OAuth client.
+client = OAuthClient.new
+client.oauth_config.consumer_key = consumer_key
+client.oauth_config.consumer_secret = consumer_secret
+client.oauth_config.signature_method = 'HMAC-SHA1'
+client.oauth_config.http_method = :get # Twitter does not allow :post
+client.debug_dev = STDERR if $DEBUG
+
+# Get request token.
+res = client.get_request_token(request_token_url, callback)
+p res.status
+p res.oauth_params
+p res.content
+p client.oauth_config
+token = res.oauth_params['oauth_token']
+secret = res.oauth_params['oauth_token_secret']
+raise if token.nil? or secret.nil?
+
+# You need to confirm authorization out of band.
+puts
+puts "Go here and do confirm: #{oob_authorize_url}?oauth_token=#{token}"
+puts "Type oauth_verifier/PIN (if given) and hit [enter] to go"
+verifier = gets.chomp
+verifier = nil if verifier.empty?
+
+# Get access token.
+# FYI: You may need to re-construct OAuthClient instance here.
+# In normal web app flow, getting access token and getting request token
+# must be done in different HTTP requests.
+# client = OAuthClient.new
+# client.oauth_config.consumer_key = consumer_key
+# client.oauth_config.consumer_secret = consumer_secret
+# client.oauth_config.signature_method = 'HMAC-SHA1'
+# client.oauth_config.http_method = :get # Twitter does not allow :post
+res = client.get_access_token(access_token_url, token, secret, verifier)
+p res.status
+p res.oauth_params
+p res.content
+p client.oauth_config
+id = res.oauth_params['user_id']
+
+puts
+puts "Access token usage example"
+puts "Hit [enter] to go"
+gets
+
+# Access to a protected resource. (DM)
+puts client.get("http://twitter.com/direct_messages.json")

0 comments on commit bc6fc4f

Please sign in to comment.