Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

use bitbucket api to get and create repository

huanqiu September 25, 2024

I want use python to get and create repository, and i have create a new App passwords with full auth. I always get wrong status code 401, please help me.

 

my python code:

 

username = "xxx"

app_password = "xxx"

bitbucket_api_url = "https://api.bitbucket.org/2.0/repositories/"

 

def check_bitbucket_repo(repo_slug):

        response = requests.get(urljoin(bitbucket_api_url, f"{bitbucket_workspace}/{repo_slug}"), auth=(username, app_password)) #, proxies=proxies #return response.status_code == 200

        if response.status_code == 200:

              log_message(f"Repository {repo_slug} exists in Bitbucket")

              return True

        else:

        log_message(f"Repository {repo_slug} does not exist in Bitbucket. Status code: {response.status_code}")

        return False

 

2 answers

1 vote
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 26, 2024

Hey @huanqiu ,

You may be getting 404 response because of multiple reasons:

  • the URL being invalid
  • the body payload being invalid
  • the user authenticating does not have permissions to create repositories in that workspace/project
  • you are trying to create a public repository inside a private project.
  • you're using the GET operation, where POST should actually be used to create the repo.

That being said, I was able to successfully create a repository using the below example python script :

 


import
requests

import json

url = "https://api.bitbucket.org/2.0/repositories/WORKSPACE_SLUG/my_new_repo"

payload = json.dumps({

"scm": "git",

"is_private": True,

"project": {

"key": "PROJ2"

}

})

headers = {

'Content-Type': 'application/json',

'Authorization': 'Basic <base 64 encoded username:app_password>'

}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

You can use this code as a reference and update yours accordingly, making sure the payload of the request and URL are similar to the above example.

If it still doesn't work, I'd recommend testing the request in a more simple tool, like curl, to validate if everything is configured correctly (authentication, URL, request body, etc). Once you get it working on curl, you can move the same data to your script.

I hope that helps! Should you have any questions, let us know!

Thank you, @huanqiu !

Patrik S

 

0 votes
huanqiu September 25, 2024

new question,  i use right username and app_password, the 401 issue is fixed, but i got status:404 when i create repository

Failed to create repository test-bitbucket . Status code: 404

You may not have access to this repository or it no longer exists in this workspace. If you think this repository exists and you have access, make sure you are authenticated.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin Site Admin
TAGS
AUG Leaders

Atlassian Community Events