Created
February 13, 2011 06:37
-
-
Save mdaisuke/824499 to your computer and use it in GitHub Desktop.
get_request_token.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openssl' | |
require 'uri' | |
require 'net/http' | |
require 'yaml' | |
require 'pp' | |
def random_str | |
a = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a | |
(Array.new(16){a[rand(a.size)]}).join | |
end | |
def sort_and_concat(params) | |
params.sort.map{|i|i.join("=")}.join("&") | |
end | |
def escape(str) | |
URI.escape(str,Regexp.new("[^a-zA-Z0-9._-]")) | |
end | |
def get_signature(params,method,url,oauth_token_secret=nil) | |
param_str = sort_and_concat(params) | |
value = [method,escape(url),escape(param_str)].join("&") | |
signature_key = "#{CONSUMER_SECRET}&#{oauth_token_secret.nil? ? "" : oauth_token_secret}" | |
sha1 = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, signature_key, value) | |
[sha1].pack('m').gsub(/\n/,'') | |
end | |
app_info = YAML.load_file 'consumer_info.yml' | |
CONSUMER_KEY = app_info['consumer_key'] | |
CONSUMER_SECRET = app_info['consumer_secret'] | |
AUTHORIZE_URL = app_info['authorize_url'] | |
REQUEST_TOKEN_URL = app_info['request_token_url'] | |
ACCESS_TOKEN_URL = app_info['access_token_url'] | |
METHOD = 'GET' | |
params = { | |
"oauth_consumer_key" => CONSUMER_KEY, | |
"oauth_signature_method" => "HMAC-SHA1", | |
"oauth_timestamp" => Time.now.to_i.to_s, | |
"oauth_nonce" => random_str, | |
"oauth_version" => "1.0" | |
} | |
params['oauth_signature'] = get_signature(params,METHOD,REQUEST_TOKEN_URL) | |
after_request = nil | |
uri = URI.parse(REQUEST_TOKEN_URL) | |
#proxy_class = Net::HTTP::Proxy(ARGV[0], 8080) | |
proxy_class = Net::HTTP::Proxy(nil, 8080) | |
http = proxy_class.new(uri.host) | |
http.start do |h| | |
param = sort_and_concat(params) | |
res = h.get("#{uri.path}?#{param}") | |
if res.code == "200" | |
puts res.code | |
after_request = Hash[*res.body.split("&").map{|i|i.split("=")}.flatten] | |
res.body =~ /(oauth_token=.*?)&/ | |
puts "access: #{AUTHORIZE_URL}?#{$1}" | |
system("open #{AUTHORIZE_URL}?#{$1}") | |
else | |
puts res.code | |
puts res.body | |
end | |
end | |
oauth_token = after_request['oauth_token'] | |
oauth_token_secret = after_request['oauth_token_secret'] | |
print "PIN > " | |
pin = gets.chomp | |
params['oauth_verifier'] = pin | |
params['oauth_token'] = oauth_token | |
params['oauth_signature'] = get_signature(params,METHOD,ACCESS_TOKEN_URL,oauth_token_secret) | |
pp params | |
uri = URI.parse(ACCESS_TOKEN_URL) | |
#proxy_class = Net::HTTP::Proxy(ARGV[0], 8080) | |
proxy_class = Net::HTTP::Proxy(nil, 8080) | |
http = proxy_class.new(uri.host) | |
http.start do |h| | |
param = sort_and_concat(params) | |
res = h.get("#{uri.path}?#{param}") | |
if res.code == "200" | |
puts res.code | |
puts res.body | |
else | |
puts res.code | |
puts res.body | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment