0

I wanted to get a list of repositories in a workspace of BitBucket and found out such a command.

curl --request GET   --url 'https://api.bitbucket.org/2.0/repositories/{workspace}'   --header 'Authorization: Bearer {token}'   --header 'Accept: application/json' 

I use a free plan of BitBucket and use for "Authorization" Basic authentication Because I had problems with oauth2. And now I use th

curl --request GET   --url 'https://api.bitbucket.org/2.0/repositories/{workspace}'   --header 'Authorization: Basic {app password}'   --header 'Accept: application/json'

But still getting such an error: Uauthorized or Token is invalid or not supported for this endpoint Has anyone faced such an issue or do you know any way how to perform that using any other ways (python ) ?

  • Are you just malformatting your basic auth payload? For example not base64 encoding the {username}:{password}? See https://stackoverflow.com/questions/25969196/how-to-define-the-basic-http-authentication-using-curl-correctly – ccchoy Oct 21 '22 at 17:58

1 Answers1

1

Make sure your auth header is properly constructed. From bitbucket docs:

Supplying Basic Auth headers

  1. Build a string of the form username:password BASE64 encode the string
  2. Supply an "Authorization" header with content "Basic " followed by the
  3. encoded string, e.g. Basic YWRtaW46YWRtaW4=

curl also comes built-in with helpers to do this for you with -u switch also in the docs page linked above:

curl -D- -u fred:fred -X GET -H "Content-Type: application/json" http://localhost:7990/rest/api/1.0/projects

With python

Alternatively if you want to do this with python, you can do something like this with the requests library(docs) or something similar with another library like httpx (docs):

from requests.auth import HTTPBasicAuth
basic = HTTPBasicAuth('user', 'pass')
requests.get('https://httpbin.org/basic-auth/user/pass', auth=basic)
# <Response [200]>

In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand for using it:

requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass'))
# <Response [200]>
ccchoy
  • 704
  • 7
  • 16