Request Bitbucket API with an access token

jenkins bitbucket oauth
bitbucket oauth resource removed
bitbucket personal access token jenkins
bitbucket consumer key
bitbucket api app password
bitbucket redirect uri
bitbucket pull request api
bitbucket login url

I'm trying to call Bitbucket API in my python script to retrieve some data. I use the key/secret pair of Bitbucket OAuth.

data = { 'grant_type': 'client_credentials'}
response = requests.post('https://bitbucket.org/site/oauth2/access_token', data=data, auth=(key, secret))
print(response.json())
access_token = response.json()['access_token']
print(access_token)
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={"Bearer %s" %access_token})
print(groups.json())

This gives me an error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/vagrant/projects/tools/axb-dsi-api/app/bitbucket_connector.py", line 35, in get_groups
    groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={"Bearer %s" %access_token})
  File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 494, in request
    prep = self.prepare_request(req)
  File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 437, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 306, in prepare
    self.prepare_headers(headers)
  File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 438, in prepare_headers
    for header in headers.items():
AttributeError: 'set' object has no attribute 'items'

How do we use access token to call BitBucket API REST ?

EDIT After correcting the header to json :

now it's saying

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/vagrant/projects/tools/axb-dsi-api/app/bitbucket_connector.py", line 36, in get_groups
    print(groups.json())
  File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

the headers parameter to the requests.get() should be an dictionary and you are sending set instead of the dictionary check your code it is headers={"Bearer %s" %access_token} which treated as set you can execute this on python and check.

You may need to use key:value check below example:

groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={'access_token' : "Bearer %s" %access_token})

Bitbucket Cloud OAuth 2.0, I would like to create a pull request comment by using Bitbucket's REST API. use an API key or an OAuth token of a technical user as it is possible with Github. Request Bitbucket API with an access token. Ask Question Asked 1 year, 5 months ago. Active 7 months ago. Viewed 713 times 0. I'm trying to

I successfuly get to retreive the data from Bitbucket API by passing the access token this way:

groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/access_token={%s}"%access_token)

Solved: Bitbucket REST API: POST using token instead of ba, Use your >refresh token to obtain a new access token."}} I tried using consumer key creation but I am not sure where to plug those values in the  Personal access tokens are a secure way to use scripts and to integrate external applications with Bitbucket Server. We recommend only mapping one token per integration. This way, if the system is compromised, you can simply revoke the token and not affect other integrations.

Use 'Authorization' as key in the header instead of 'acess_token' try below code.

groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={'Authorization' : "Bearer %s" %access_token})

REST Resources Provided By: Bitbucket Server, This page describes how to create credentials for your Git host (GitHub, GitLab, Bitbucket, or Azure DevOps). that Atlantis will use to make API calls. Create an Atlantis user (optional); Generating an Access Token a token; Name the token atlantis; Give the token Read Project permissions and Write Pull request permissions  Now be aware that OAuth 2 tokens expire in an hour. After that they will cease to work. Depending on how you obtained the access token you may have a refresh token. You can use this refresh token to issue a new access token immediately before attempting to clone, to avoid expiration.

How to authenticate a request in Bitbucket REST API, Credentials you get from adding a new consumer in bitbucket -> manage account from bitbucket>' >>> # OAuth endpoints given in the Bitbucket API documentation Fetch a request token >>> from requests_oauthlib import OAuth1Session  Dear Atlassian, I created an ASP.NET MVC application which can authorize the user at Bitbucket. I used CSharp.Bitbucket library to get the token secret and token value. The OAuth tutorial said that with the token I can make API calls. I know that I can call the API using basic authorization like thi

Git Host Access Credentials, Code against the Bitbucket API to automate simple tasks, embed Bitbucket data into your own site, build mobile or desktop apps, or even add custom UI add-ons​  You can see the different ways of using an OAuth token under "Making Requests" on Bitbucket OAuth 2.0. The best way is to pass your token in the Authorization header, like so: Authorization: Bearer {access_token} Note also that you shouldn't need to pass your secret as part of the request, just the token. cheers, Tim

Bitbucket OAuth 1 Tutorial, Starting with Bitbucket 5.5 (server and data center), you can use personal access tokens many CLI actions and REST API requests instead of  That will redirect to the callback URL with a fragment containing the access token (#access_token={token}&token_type=bearer) where your page's JavaScript can pull it out of the URL. Making requests. Once you have an access token, as per RFC-6750, you can use it in a request in any of the following ways (listed from most to least desirable):

Comments
  • please check my updated question. I get another error
  • please check if you are getting any response in groups
  • Yes I get to print the access token, but can't print the json of the second request
  • I think you are not getting any response for second request i.e. groups
  • That's strange. I can get a response when I use my username password for authentication but not when I use an access token