Permalink
Browse files

Updated the oauth tweet example so it follows the middleware concept.

  • Loading branch information...
1 parent 2512106 commit 8609fedcc70a28e965076c2cde0f40d12927c046 Sidharta Surya Kusnanto committed Mar 8, 2011
Showing with 69 additions and 38 deletions.
  1. +69 −38 examples/oauth-tweet.rb
View
@@ -1,50 +1,81 @@
-# Courtesy of Darcy Laycock:
-# http://gist.github.com/265261
-#
-
require 'rubygems'
+require 'eventmachine'
+require 'em-http-request'
+require 'oauth/helper'
+require 'oauth/signature/hmac/sha1'
+require 'uri'
-require 'em-http'
-require 'oauth'
+module TwitterDemo
+ STREAM_URL = "http://twitter.com/statuses/update.json"
+ HTTP_METHOD = "POST"
-# At a minimum, require 'oauth/request_proxy/em_http_request'
-# for this example, we'll use Net::HTTP like support.
-require 'oauth/client/em_http'
+ CONSUMER_KEY = ""
+ CONSUMER_SECRET = ""
+ ACCESS_TOKEN = ""
+ ACCESS_TOKEN_SECRET = ""
+ SIGNATURE_METHOD = "HMAC-SHA1"
+ OAUTH_VERSION = "1.0"
+ TWEET = "Hello Twitter from em-http-request with OAuth"
-# You need two things: an oauth consumer and an access token.
-# You need to generate an access token, I suggest looking elsewhere how to do that or wait for a full tutorial.
-# For a consumer key / consumer secret, signup for an app at:
-# http://twitter.com/apps/new
+ class OAuthMiddleware
+
+ def self.normalize_string(raw_string)
+ unsafe = /[^a-zA-Z0-9\-_\.~]+|[\&]+/i
+ URI.escape(raw_string, unsafe)
+ end
-# Edit in your details.
-CONSUMER_KEY = ""
-CONSUMER_SECRET = ""
-ACCESS_TOKEN = ""
-ACCESS_TOKEN_SECRET = ""
+ def self.generate_base_string(http_method, url, parameters)
+ safe_parameters = normalize_string parameters
+ safe_http_method = normalize_string http_method
+ safe_url = normalize_string url
+ end
-def twitter_oauth_consumer
- @twitter_oauth_consumer ||= OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
-end
+ def self.sign(key, base_string)
+ digest = OpenSSL::Digest::Digest.new("sha1")
+ hmac = OpenSSL::HMAC.digest(digest, key, base_string)
+ Base64.encode64(hmac).chomp.gsub(/\n/, '')
+ end
-def twitter_oauth_access_token
- @twitter_oauth_access_token ||= OAuth::AccessToken.new(twitter_oauth_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
-end
+ def self.request(head, body)
+ oauth_nonce = OAuth::Helper.generate_key
+ oauth_signature_method = "HMAC-SHA1"
+ oauth_timestamp = OAuth::Helper.generate_timestamp
+
+ raw_oauth_parameters = "oauth_consumer_key=#{CONSUMER_KEY}&" + \
+ "oauth_nonce=#{oauth_nonce}&" + \
+ "oauth_signature_method=#{oauth_signature_method}&" + \
+ "oauth_timestamp=#{oauth_timestamp}&" + \
+ "oauth_token=#{ACCESS_TOKEN}&" + \
+ "oauth_version=#{OAUTH_VERSION}"
-EM.run do
+ body.each do |k, v|
+ raw_oauth_parameters << %{&#{k}=} << self.normalize_string(%{#{v}})
+ end
- request = EventMachine::HttpRequest.new('http://twitter.com/statuses/update.json')
- http = request.post(:body => {'status' => 'Hello Twitter from em-http-request with OAuth'}, :head => {"Content-Type" => "application/x-www-form-urlencoded"}) do |client|
- twitter_oauth_consumer.sign!(client, twitter_oauth_access_token)
- end
+ secret_key = "#{CONSUMER_SECRET}&#{ACCESS_TOKEN_SECRET}"
+ oauth_signature = self.normalize_string( self.sign(secret_key, self.generate_base_string(HTTP_METHOD, STREAM_URL, raw_oauth_parameters)) ).gsub("=","%3D")
- http.callback do
- puts "Response: #{http.response} (Code: #{http.response_header.status})"
- EM.stop_event_loop
- end
+ head["authorization"] = %{OAuth oauth_consumer_key="#{CONSUMER_KEY}", oauth_token="#{ACCESS_TOKEN}",oauth_signature_method="#{oauth_signature_method}", oauth_timestamp="#{oauth_timestamp}", oauth_nonce="#{oauth_nonce}", oauth_signature="#{oauth_signature}", oauth_version="#{OAUTH_VERSION}"}
- http.errback do
- puts "Failed to post"
- EM.stop_event_loop
- end
+ [head,body]
+ end
+ end
+
+ EM.run do
+ @request = EventMachine::HttpRequest.new(STREAM_URL)
+ @request.use OAuthMiddleware
+ http = @request.post :body =>{"status" => TWEET}, :head => {"Accept" => "*/*", "User-Agent" => "propertweet/0.1", "Keep-alive" => "true" }
+
+ http.callback do
+ puts http.response
+ EM.stop_event_loop
+ end
+
+ http.errback do
+ puts "Failed retrieving user stream."
+ puts http.response
+ EM.stop_event_loop
+ end
+ end
+end
-end

0 comments on commit 8609fed

Please sign in to comment.