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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.