Ruby 使用oAuth令牌进行API调用

Ruby 使用oAuth令牌进行API调用,ruby,oauth,Ruby,Oauth,我想用Ruby提出oAuth请求。我浏览了一些示例,但没有人使用oauth\u-token\u-secret和oauth\u-token来发出请求,他们只使用consumer\u-key和consumer\u-secret来获取oauth\u-token\u-secret。但是我已经有了oauth\u-token\u-secret和oauth\u-token # Initialisation based on string values: consumer_key = 'AVff2raXvhMU

我想用Ruby提出oAuth请求。我浏览了一些示例,但没有人使用
oauth\u-token\u-secret和oauth\u-token
来发出请求,他们只使用
consumer\u-key
consumer\u-secret
来获取
oauth\u-token\u-secret
。但是我已经有了
oauth\u-token\u-secret
oauth\u-token

# 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)
举个例子,我试着用这个

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
在我的例子中,有4个密钥:

consumer_key = "anonymous"
consumer_secret = "anonymous"
oauth_token_secret = "fdsfdsfdfdsfds"
oauth_token = "fdsfdsfdfdsfdsdsdsdsdsdsds"
所以我需要做的是,使用一些额外的get参数和oAuth令牌向特定的url发出API请求,并获取答案

我用Ruby怎么做

#!/usr/bin/env ruby
require 'rubygems'
require 'oauth'
require 'json'
您需要获取访问令牌(
OAuth::AccessToken

一旦拥有了
OAuth::AccessToken
对象,您就可以:

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

响应是一个json对象。要阅读它,您可以执行以下操作:

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

我不需要使用OAuth::AccessToken,因为我已经有了OAuth_secret和OAuth_令牌。@AlanDert我知道。使用您在此处获得的对象:
access\u-token=request\u-token.get\u-access\u-token(:oauth\u-verifier=>verifier)
什么是verifier?你能给出完整的例子吗?注意,Typhous有一个错误,我不能使用它。我如何为oAuth2这样做?请再给我看一次。我在GitHub看到了这些示例,但是AccessToken的构造函数与第一个版本中的不同。