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
Hey @huanqiu ,
You may be getting 404 response because of multiple reasons:
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
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.
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.