Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Can't create a repository with BitBucket API

Francesco Camussoni November 15, 2024

Hi! I'm using the example of the BitBucket API post method to create a repository 

https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-post  



I'm using a personal token that I created on BitBucket Personal Settings > Apps Passwords with full permissions.

My user can create repositories within the UI of BitBucket, but when I run the code I got this error:

{  "error": {  "message": "Token is invalid or not supported for this endpoint."  },  "type": "error" }
# This code sample uses the 'requests' library:
import requests
import json

workspace = "workspace-id"
project = "ptoject-key"
repo_name = "test"
personal_token = os.environ['ACCESS_TOKEN']
local_repo_dir = "test" # Set your desired local path here

url = "https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}"

headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ['ACCESS_TOKEN']}"
}

payload = {
"scm": "git",
"project": {"key": project},
"is_private": True
}

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

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

1 answer

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 18, 2024

Hi Francesco,

An app password is a type of password, it cannot be used as a Bearer access token. It needs to be used with Basic authentication along with the account's username. You can find the username of your Bitbucket account on this page (after you log in):

If you base64-encode the string username:app_password (where username and app_password should be your own values) and the base64-encoded value is xxxxxxx, you can use it in the header as follows:

headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Basic xxxxxxx',
}

Just a heads up, I removed the workspace ID and project name from your post for privacy reasons.

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events