【问题标题】:Can't generate an OAuth1 Authorization header using Typhoeus in Ruby无法在 Ruby 中使用 Typhoeus 生成 OAuth1 授权标头
【发布时间】:2016-04-28 15:00:03
【问题描述】:

我正在尝试使用 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::HTTPGenericRequestHash。 Typhoeus 文档表明这应该可以工作,所以我可能做错了什么。

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

【问题讨论】:

    标签: ruby oauth typhoeus


    【解决方案1】:

    您需要明确要求 RequestProxy。

    require 'typhoeus'
    require 'oauth'
    require 'oauth/request_proxy/typhoeus_request'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 2021-01-30
      • 2020-05-22
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 2018-08-13
      相关资源
      最近更新 更多