3

我正在尝试使用 Ruby 通过 Typhoeus 向 Bitbucket 的 API 发送请求,并且我正在使用来自“OAuth 消费者”页面的凭据 -consumer_keyconsumer_secret. 我可以很好地获取令牌,但是在尝试从 OAuth 帮助程序生成标头时出现错误。这是相关代码(注意这不是 Rails;我使用的是 Sinatra):

# oauth_token and oauth_token_secret are from a request to https://bitbucket.org/api/1.0/oauth/request_token
@consumer = OAuth::Consumer.new(
  consumer_key,
  consumer_secret,
)
@token = OAuth::Token.new(oauth_token, oauth_token_secret)
options = {
  method: :post,
  body: body.to_json
}

oauth_params = {:consumer => @consumer, :token => @token}

hydra = Typhoeus::Hydra.new
url = "https://api.bitbucket.org/2.0/repositories/..."
req = Typhoeus::Request.new(url, options)
oauth_helper = OAuth::Client::Helper.new(req, oauth_params.merge(:request_uri => url))
req.options[:headers].merge!({"Authorization" => oauth_helper.header})

最后一行因OAuth::RequestProxy::UnknownRequestType错误而失败。基本上它的意思是 OAuth RequestProxy 不理解该Typhoeus::Request对象;据我所知,它只支持Net::HTTPGenericRequestand Hash。Typhoeus 文档表明这应该可以工作,所以我可能做错了什么。

或者,有没有更好的方法来做到这一点?我应该使用不同的请求库吗?HTTParty 对这样的 auth 标头没有很好的支持。谢谢!

4

1 回答 1

2

您需要明确要求 RequestProxy。

require 'typhoeus'
require 'oauth'
require 'oauth/request_proxy/typhoeus_request'
于 2016-07-22T02:51:43.470 回答