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

BitBucket Cloud API Use

Banana-Pilot
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 29, 2024

Hi, I need to use the Bitbucket API to create Pull requests and am using the Atlassian-python library. But I am running into a 401 error when I run this block of code:

 

import logging
import traceback
from atlassian import Bitbucket

# Set up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

bitbucket_app_pw = Bitbucket(
username='x@gmail.com',
password="xxxx", # I used the app password
cloud=True
)

def create_pull_request(source_project: str, source_repo: str, dest_project: str, dest_repo: str,
source_branch: str, destination_branch: str, title: str, description: str) -> str:
logger.info("Attempting to create pull request...")
try:
logger.debug(f"Parameters: source_project={source_project}, source_repo={source_repo}, "
f"dest_project={dest_project}, dest_repo={dest_repo}, "
f"source_branch={source_branch}, destination_branch={destination_branch}, "
f"title={title}, description={description}")
pr = bitbucket_app_pw.open_pull_request(
source_project,
source_repo,
dest_project,
dest_repo,
source_branch,
destination_branch,
title,
description
)
logger.info(f"API call successful. PR data: {pr}")
return f"Pull request '{title}' created with ID: {pr['id']}"
except Exception as e:
logger.error(f"Exception occurred: {str(e)}")
logger.error("Traceback:", exc_info=True)
return f"Failed to create pull request: {str(e)}"

# When calling the function
try:
result = create_pull_request("SourceProj", "ApiDest", "SourceProj", "ApiDest", "test2", "test3", "TEST", "This is a Test. Next Steps now.")
logger.info(f"Function returned: {result}")
except Exception as e:
logger.error(f"Unexpected error occurred: {str(e)}")
logger.error("Traceback:", exc_info=True)

This was the last line of the error:

 

File "/Users/.../Library/Python/3.9/lib/python/site-packages/atlassian/rest_client.py", line 531, in raise_for_status raise HTTPError("Unauthorized (401)", response=response) requests.exceptions.HTTPError: Unauthorized (401) 2024-07-29 17:17:32,390 - INFO - Function returned: Failed to create pull request: Unauthorized (401)

 

I am running into some kind of authentication issue, so should I instead switch to using the Oauth way mentioned here: https://atlassian-python-api.readthedocs.io/index.html?

 

1 answer

1 accepted

1 vote
Answer accepted
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 30, 2024

I may be wrong, but to me it seems that the library at https://pypi.org/project/atlassian-python-api/ is for Bitbucket Data Center!

Bitbucket Cloud has a very different REST API, so I'd think that you should check if this Python lib is compatible with it at all.

Banana-Pilot
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 30, 2024

Yes, you're right I decided to go with the default Bitbucket Cloud Rest API. Thanks.

Like Aron Gombas _Midori_ likes this

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