greggilbert
greggilbert

Reputation: 1363

Can't generate an OAuth1 Authorization header using Typhoeus in Ruby

I'm trying to send a request via Typhoeus to Bitbucket's API using Ruby, and I'm doing so with credentials from the "OAuth consumers" page - the consumer_key and consumer_secret. I can get the token just fine, but I get an error when trying to generate the header from the OAuth helper. Here's the relevant code (note that this is not Rails; I'm using 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})

The last line fails with a OAuth::RequestProxy::UnknownRequestType error. Basically what it's saying is that the OAuth RequestProxy doesn't understand the Typhoeus::Request object; from what I can see, it only has support for Net::HTTPGenericRequest and Hash. The Typhoeus documentation indicates that this should work, so it's possible I'm doing something wrong.

Alternatively, is there a better way to do this? Should I use a different request library? HTTParty doesn't have great support for auth headers like this. Thanks!

Upvotes: 3

Views: 831

Answers (1)

gcstr
gcstr

Reputation: 1547

You need to explicitly require the RequestProxy.

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

Upvotes: 3

Related Questions