Bitbucket cloud APIs have a good documentation, but I could not find a comprehensive examples showing how to make authenticated calls to those APIs.
My requirement is very simple. As a bitbucket cloud admin for a given workspace, I would like to get a list of all the repos the workspace has.
I would like to be able to do this from my python backend (a simple script & nothing else). There is no user interaction involved in this case. Now having gone through the documentation & a bunch of online resources, it appears that there are a few ways to get authentication tokens, which is (obviously) the first step to be able to call any other APIs.
Here are a few things I'm struggling with & would really appreciate any inputs around the same : (PS: pointing me to the right resources would also be great help. Don't necessarily need direct spoon fed answers :) )
bitbucket_app_pw = Cloud( url='https://api.bitbucket.org/', username=bitbucket_username, password=bitbucket_app_password, cloud=True)
and then the corresponding API call to list the repos seems to be
bitbucket.project_list()
but the bitbucket Cloud object (in this case bitbucket_app_pw), obtained from the former snippet above, does not seem to have the project_list method at all. Am I missing something here?
About the part of your question concerning project_list(): you need to get a workspace object in order to get a list of projects.
So you could do something like this:
bitbucket.workspaces.get(<my_workspace_name_here>).projects.each()
and you would get a generator for project objects.
This has been working for me using the app-password authentication you mentioned in #3.
About #2, the endpoint you should be using is:
https://api.bitbucket.org/2.0/repositories/<my_workspace_name_here>
curl -u username:app_password https://api.bitbucket.org/2.0/repositories/<my_workspace_name_here>
I don't know about #1.
Hello @abhinav.mr.impractical , by using the username and app password as @Marie-Hélène Touchette suggests, you are able to eliminate the need for an access token. This does not directly answer your question#1, but it may help. See the App passwords section of the Authentication Methods page.
For example, if your workspace is companyname, your username is prospero , and the app password you created is a1b2c3d4e5f6g7h8, then you can retrieve the list of repositories in this manner.
Curl:
curl -u prospero:a1b2c3d4e5f6g7h8 https://api.bitbucket.org/2.0/repositories/companyname
Python:
import requests
import json
r = requests.get('https://api.bitbucket.org/2.0/repositories/companyname', auth=('prospero', 'a1b2c3d4e5f6g7h8'))
print(r.text)
print(json.dumps(r.json(), indent=4))
Of course, you will want to use more sophisticated code. These are just quick examples.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.