Skip to content

Instantly share code, notes, and snippets.

@nov
Created December 3, 2009 14:02
Show Gist options
  • Save nov/248176 to your computer and use it in GitHub Desktop.
Save nov/248176 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'oauth'
htt_method = :get
endpoint = 'http://oauth.service-provider.com/oauth/request_token'
consumer_secret = 'CONSUMER_SECRET'
token_secret = 'ACCESS_TOKEN_SECRET'
request_params = {
# ADD ALL PARAMETERS HERE
# ex.) In case of OpenSocial OAuth proxy
# :xoauth_app_url => "http://example.com/gadget.xml",
# :opensocial_owner_id => "nov",
# :opensocial_viewer_id => "nov",
# :opensocial_app_id => "http://example.com/gadget.xml",
# :opensocial_app_url => "http://example.com/gadget.xml"
}
oauth_params = {
:oauth_consumer_key => 'CONSUMER_KEY',
:oauth_token => 'REQUEST_TOKEN_OR_ACCESS_TOKEN_OR_BLANK',
:oauth_verifier => 'VERIFIER',
:oauth_nonce => 'NONCE',
:oauth_timestamp => 'TIMESTAMP',
:oauth_signature_method => 'HMAC-SHA1',
:oauth_version => '1.0'
}
def escape(value)
URI::escape(value.to_s, OAuth::RESERVED_CHARACTERS)
end
def signature_key(consumer_secret, token_secret)
"#{escape consumer_secret}&#{escape token_secret}"
end
def signature_base_string(http_method, endpoint, params = {})
sorted_params = params.sort do |a, b|
a[0].to_s <=> b[0].to_s or a[1].to_s <=> b[1].to_s
end
params_string = sorted_params.collect do |key, value|
"#{escape key}=#{escape value}"
end.join("&")
"#{http_method.to_s.upcase}&#{escape endpoint}&#{escape params_string}"
end
signature_key = signature_key(consumer_secret, token_secret)
signature_base_string = signature_base_string(htt_method, endpoint, oauth_params.merge(request_params))
digest = HMAC::SHA1.digest(signature_key, signature_base_string)
signature = escape Base64.encode64(digest).chomp.gsub(/\n/,'')
puts <<-EXPECTED
SIGNATURE BASE STRING
------------------------
#{signature_base_string}
SIGNATURE KEY
------------------------
#{signature_key}
SIGNATURE
------------------------
#{signature}
EXPECTED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment