bitbucket-rest-api / bitbucket

BitBucket API gem - bitbucket_rest_api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Webhook creation fails with BitBucket::Error::Unauthorized

ispyropoulos opened this issue · comments

When attempting to create a webhook via the gem, e.g.

client.repos.webhooks.create('repo_owner', 'repo_name', description: "Test Webhook", url: 'http://www.example.com/callbacks/bitbucket', active: false, events: ['repo:push'])

results in

BitBucket::Error::Unauthorized: #<struct Faraday::Env method=:post, body="", url=#<URI::HTTPS https://api.bitbucket.org/2.0/repositories/testributor/katana/hooks>, request=#<Faraday::RequestOptions oauth=true>, request_headers={"User-Agent"=>"BitBucket Ruby Gem 0.1.7", "Content-Type"=>"application/x-www-form-urlencoded", "Authorization"=>"OAuth oauth_consumer_key=\"7CjsrYVQ9SCdTioSU3v\", oauth_nonce=\"bafb5f3924d2f606c4ed3beaaf2b1f29\", oauth_signature=\"Mdm%2BjIqfceg%2BdZ1l887nad%2BcmyY%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1459954879\", oauth_token=\"4XqGMTY2UnyFnhcudm\", oauth_version=\"1.0\""}, ssl=#<Faraday::SSLOptions (empty)>, parallel_manager=nil, params=nil, response=#<Faraday::Response:0x007fdbf0376878 @on_complete_callbacks=[], @env=#<Faraday::Env @method=:post @body="" @url=#<URI::HTTPS https://api.bitbucket.org/2.0/repositories/testributor/katana/hooks> @request=#<Faraday::RequestOptions oauth=true> @request_headers={"User-Agent"=>"BitBucket Ruby Gem 0.1.7", "Content-Type"=>"application/x-www-form-urlencoded", "Authorization"=>"OAuth oauth_consumer_key=\"7CjsrYVQ9SCdTioSU3v\", oauth_nonce=\"bafb5f3924d2f606c4ed3beaaf2b1f29\", oauth_signature=\"Mdm%2BjIqfceg%2BdZ1l887nad%2BcmyY%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1459954879\", oauth_token=\"4XqGMTY2UnyFnhcudm\", oauth_version=\"1.0\""} @ssl=#<Faraday::SSLOptions (empty)> @response=#<Faraday::Response:0x007fdbf0376878 ...> @response_headers={"server"=>"nginx/1.6.2", "vary"=>"Cookie", "www-authenticate"=>"Basic realm=\"Bitbucket.org HTTP\"", "content-type"=>"text/html; charset=utf-8", "strict-transport-security"=>"max-age=31536000", "date"=>"Wed, 06 Apr 2016 15:01:20 GMT", "x-served-by"=>"app-113", "x-static-version"=>"195aa3f25c29", "etag"=>"\"d41d8cd98f00b204e9800998ecf8427e\"", "x-render-time"=>"0.0133571624756", "connection"=>"close", "x-version"=>"195aa3f25c29", "x-request-count"=>"378", "x-frame-options"=>"SAMEORIGIN", "content-length"=>"0"} @status=401>>, response_headers={"server"=>"nginx/1.6.2", "vary"=>"Cookie", "www-authenticate"=>"Basic realm=\"Bitbucket.org HTTP\"", "content-type"=>"text/html; charset=utf-8", "strict-transport-security"=>"max-age=31536000", "date"=>"Wed, 06 Apr 2016 15:01:20 GMT", "x-served-by"=>"app-113", "x-static-version"=>"195aa3f25c29", "etag"=>"\"d41d8cd98f00b204e9800998ecf8427e\"", "x-render-time"=>"0.0133571624756", "connection"=>"close", "x-version"=>"195aa3f25c29", "x-request-count"=>"378", "x-frame-options"=>"SAMEORIGIN", "content-length"=>"0"}, status=401>

Now, the response code is misleading. Bitbucket's API shouldn't return 401, as there's no problem with the authorization. I retried other calls with the same keys and they work just fine.

The problem is the "Content-Type"=>"application/x-www-form-urlencoded". This should be application/json. If one attempts to replicate the same call via Postman or curl, the results are consistent. With the gem's incorrect method a 401 is returned, but with the correct method, 201 (Created) is returned.

It also seems that this 0e53c0e does not fix the problem.

@ispyropoulos Indirectly related, but how are you able to even access webhooks? Even using this gem from master I get:

BitBucket.new.repos.webhooks
NoMethodError: undefined method `webhooks' for #<BitBucket::Repos:0x00564ba5e85bd8>
from /home/razorx/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/bitbucket-13901a54882a/lib/bitbucket_rest_api/api.rb:79:in `method_missing'

The class exists:

BitBucket::Repos::Webhooks.actions
=> [:actions, :create, :delete, :edit, :get, :list]

but then it's not in the actions list:

BitBucket::Repos.actions
=> [:actions,
 :all,
 :branches,
 :changesets,
 :commits,
 :create,
 :delete,
 :download,
 :edit,
 :find,
 :following,
 :forks,
 :get,
 :keys,
 :list,
 :list_branches,
 :list_tags,
 :pull_request,
 :repo_tags,
 :repository_tags,
 :services,
 :sources,
 :tags]

Same with this:

BitBucket.new.repos.actions
=> [:actions,
 :all,
 :branches,
 :changesets,
 :commits,
 :create,
 :delete,
 :download,
 :edit,
 :find,
 :following,
 :forks,
 :get,
 :keys,
 :list,
 :list_branches,
 :list_tags,
 :pull_request,
 :repo_tags,
 :repository_tags,
 :services,
 :sources,
 :tags]

@razor-x you are absolutely right, apologies. The master branch needs this, in lib/bitbucket_rest_api/repos.rb to work:

def webhooks
  @webhooks ||= ApiFactory.new 'Repos::Webhooks'
end

I was attempting this from my fork and I didn't realise this part was missing from master when I wrote the issue.

In any case, the problem is still there. The missing #webhooks method does not change much really.

Fixed in #76.