2012-12-11 12 views
8

Chcę utworzyć żądanie OAuth w Ruby. Przesłuchałem kilka przykładów, ale żaden z nich nie użył oauth_token_secret and oauth_token, aby złożyć wniosek, użyli tylko consumer_key i consumer_secret, aby uzyskać oauth_token_secret i oauth_token. Ale mam już oauth_token_secret i oauth_token.Aby wykonać wywołanie API za pomocą tokenu OAuth

Na przykład, ten próbowałem użyć

require 'rubygems' 
require 'oauth' 
consumer = OAuth::Consumer.new(consumer_key, consumer_secret, 
           { 
           :site=> "https://www.google.com", 
           :scheme=> :header, 
           :http_method=> :post, 
           :request_token_path => "/accounts/OAuthGetRequestToken", 
           :access_token_path => "/accounts/OAuthGetAccessToken", 
           :authorize_path=> "/accounts/OAuthAuthorizeToken", 
           :signature_method=>"RSA-SHA1"}, 
           # :private_key_file=>PATH_TO_PRIVATE_KEY 
           ) 

request_token = consumer.get_request_token() 

puts "Visit the following URL, log in if you need to, and authorize the app" 
puts request_token.authorize_url 
puts "When you've authorized that token, enter the verifier code you are assigned:" 

verifier = gets.strip 

puts "Converting request token into access token..." 

access_token=request_token.get_access_token(:oauth_verifier => verifier) 

puts "access_token.token --> #{access_token.token}" # But I initially have it 
puts "access_token.secret --> #{access_token.secret}" # But I initially have it 

W moim przypadku, istnieją 4 tajnych kluczy:

consumer_key = "anonymous" 
consumer_secret = "anonymous" 
oauth_token_secret = "fdsfdsfdfdsfds" 
oauth_token = "fdsfdsfdfdsfdsdsdsdsdsdsds" 

Więc co muszę zrobić to, aby złożyć zamówienie API do określonego adresu URL z dodatkowymi parametrami pobierania i tokenem OAuth i uzyskania odpowiedzi.

Jak to zrobić w Ruby?

Odpowiedz

12
#!/usr/bin/env ruby 
require 'rubygems' 
require 'oauth' 
require 'json' 

Trzeba uzyskać access_token (OAuth::AccessToken).

# Initialisation based on string values: 
consumer_key = 'AVff2raXvhMUxFnif06g' 
consumer_secret = 'u0zg77R1bQqbzutAusJYmTxqeUpWVt7U2TjWlzbVZkA' 
access_token = 'R1bQqbzYm0zg77tAusJzbVZkAVt7U2T' 
access_token_secret = 'sVbVZkAt7U2TjWlJYmTxqR1bQqbzutAuWzeUpu0zg77' 

@consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {:site=>'http://my.site'}) 
accesstoken = OAuth::AccessToken.new(@consumer, access_token, access_token_secret) 

Gdy masz swój obiekt OAuth::AccessToken, robisz:

json_response = accesstoken.get('/photos.xml') 
# or 
json_response = accesstoken.post(url, params_hash) 

itp

Odpowiedź jest obiekt JSON. Aby go przeczytać, możesz wykonać:

response = JSON.parse(json_response.body) 
# which is a hash 
# you just access content like 
id = response["id"] 
+0

Nie muszę pobierać OAuth :: AccessToken, ponieważ mam już oauth_secret i oauth_token. –

+0

@AlanDert Wiem. Użyj obiektu, który tu uzyskasz: 'access_token = request_token.get_access_token (: oauth_verifier => verifyier)' – oldergod

+0

Co to jest weryfikator? Czy możesz podać pełny przykład? Pamiętaj, że Typhoeus ma błąd, nie mogę go użyć. –

Powiązane problemy