2016-01-22 30 views
3

我試圖通過Typhoeus向使用Ruby的Bitbucket API發送請求,並且我正在使用來自「OAuth消費者」頁面的憑證 - consumer_keyconsumer_secret。我可以得到令牌,但是在嘗試從OAuth助手生成頭時出現錯誤。下面是相關的代碼(注意,這是不是Rails的,我使用的是西納特拉):無法使用Ruby中的Typhoeus生成OAuth1授權標頭

# 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頭文件。謝謝!

回答

2

您需要明確地要求RequestProxy。

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