Created
August 20, 2014 11:03
-
-
Save loicginoux/c67745a8a3bc43eec463 to your computer and use it in GitHub Desktop.
yahoo boss test api
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 'net/http' | |
include HTTParty | |
# test app | |
YAHOO_CONFIG = { | |
client_id: "YOUR APP ID", | |
client_secret: "YOUR APP SECRET" | |
} | |
################################# | |
# get the request token | |
################################# | |
nonce = SecureRandom.hex() | |
ts=Time.now.to_i | |
q = URI.encode_www_form( | |
"oauth_nonce" => nonce, | |
"oauth_timestamp" => ts, | |
"oauth_consumer_key" => YAHOO_CONFIG[:client_id], | |
"oauth_signature" => "#{YAHOO_CONFIG[:client_secret]}&", | |
"oauth_signature_method" => "PLAINTEXT", | |
"oauth_version" => "1.0", | |
"xoauth_lang_pref" => "en-us", | |
"oauth_callback" => "oob" #http://YOUR_SITE/callback | |
) | |
response = HTTParty.get "https://api.login.yahoo.com/oauth/v2/get_request_token?#{q}" | |
p=CGI.parse(response) | |
oauth_token = p["oauth_token"].first | |
oauth_token_secret = p["oauth_token_secret"].first | |
################################# | |
# get the oauth_verifier which will be sent back to the url oauth_callback | |
################################# | |
q = URI.encode_www_form( | |
"oauth_token" => oauth_token | |
) | |
"https://api.login.yahoo.com/oauth/v2/request_auth?#{q}" | |
# when you visit the url above, copy paste the code given here | |
oauth_verifier = "XXXXX" | |
################################# | |
# get the access_token | |
################################# | |
nonce = SecureRandom.hex() | |
ts=Time.now.to_i | |
q = URI.encode_www_form( | |
"oauth_nonce" => nonce, | |
"oauth_timestamp" => ts, | |
"oauth_consumer_key" => YAHOO_CONFIG[:client_id], | |
"oauth_signature" => "#{YAHOO_CONFIG[:client_secret]}&#{oauth_token_secret}", | |
"oauth_signature_method" => "PLAINTEXT", | |
"oauth_version" => "1.0", | |
"xoauth_lang_pref" => "en-us", | |
"oauth_token" => oauth_token, | |
"oauth_verifier" => oauth_verifier | |
) | |
response = HTTParty.get "https://api.login.yahoo.com/oauth/v2/get_token?#{q}" | |
p=CGI.parse(response) | |
yahoo_access_token = p["oauth_token"].first | |
yahoo_access_token_secret = p["oauth_token_secret"].first | |
session_handle = p["oauth_session_handle"].first | |
################################# | |
# get data from API | |
################################# | |
def percent_encode( string ) | |
# ref http://snippets.dzone.com/posts/show/1260 | |
return URI.escape( string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") ).gsub('*', '%2A') | |
end | |
def signature(consumer_secret, token_secret, base_str) | |
key = percent_encode( consumer_secret ) + '&' + percent_encode( token_secret ) | |
# ref: http://blog.nathanielbibler.com/post/63031273/openssl-hmac-vs-ruby-hmac-benchmarks | |
digest = OpenSSL::Digest::Digest.new( 'sha1' ) | |
hmac = OpenSSL::HMAC.digest( digest, key, base_str ) | |
# ref http://groups.google.com/group/oauth-ruby/browse_thread/thread/9110ed8c8f3cae81 | |
Base64.encode64( hmac ).chomp.gsub( /\n/, '' ) | |
end | |
def query_string(params) | |
pairs = [] | |
params.sort.each { | key, val | | |
pairs.push( "#{ percent_encode( key ) }=#{ percent_encode( val.to_s ) }" ) | |
} | |
pairs.join '&' | |
end | |
consumer_key = YAHOO_CONFIG[:client_id] | |
consumer_secret = YAHOO_CONFIG[:client_secret] | |
token = yahoo_access_token | |
token_secret = yahoo_access_token_secret | |
req_method = "GET" | |
sig_method = "HMAC-SHA1" | |
oauth_version = "1.0" | |
url = "http://yboss.yahooapis.com/ysearch/web" | |
parsed_url = URI.parse( url ) | |
nonce = Array.new( 5 ) { rand(256) }.pack('C*').unpack('H*').first | |
params = { | |
'format' => 'json', | |
'oauth_consumer_key' => consumer_key, | |
'oauth_nonce' => nonce, | |
'oauth_signature_method' => sig_method, | |
'oauth_token' => token, | |
'oauth_timestamp' => Time.now.to_i.to_s, | |
'oauth_version' => oauth_version, | |
'q' => "YOUR SEARCH TERM" | |
} | |
if parsed_url.query | |
params.merge! CGI.parse( parsed_url.query ) | |
end | |
req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path | |
base_str = [ | |
req_method, | |
percent_encode( req_url ), | |
percent_encode( query_string(params) ) # normalization is just x-www-form-urlencoded | |
].join( '&' ) | |
params[ 'oauth_signature' ] = signature(consumer_secret, token_secret, base_str) | |
datnt_query_string= query_string (params) | |
yahoo_response=nil | |
Net::HTTP.start( parsed_url.host ) { | http | | |
req = Net::HTTP::Get.new "#{ parsed_url.path }?#{ datnt_query_string }" | |
response = http.request(req) | |
yahoo_response = response | |
response.read_body | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment