Bearer token based authentication in JIRA-Python API

abhay October 9, 2024

I am trying to use jira python library to initialise JIRA object using bearer token based authentication. I didn't find anything in the documentation.

2 answers

1 accepted

0 votes
Answer accepted
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 9, 2024

Hi, @abhay 

Welcome to Atlassian Community!

In my python script, when I connect to Jira, I use next code:

# Load environment variables from .env file
jira_base_url = config("JIRA_BASE_URL")
jira_api_token = config("JIRA_API_TOKEN")
jira_api_email = config("JIRA_API_EMAIL")

# Jira API endpoint for searching issues
jira_api_url = f"{jira_base_url}/rest/api/3/search"
base64_credentials = base64.b64encode(
f"{jira_api_email}:{jira_api_token}".encode()
).decode()
headers = {
"Authorization": f"Basic {base64_credentials}",
"Content-Type": "application/json",
}


Example of function, which I use to get issues by JQL query

def get_jira_issues(query, max_results=50):
issues = []
start_at = 0

while True:
params = {
"jql": query,
"fields": ["comment"],
"startAt": start_at,
"maxResults": max_results,
}
response = requests.get(jira_api_url, headers=headers, params=params)

if response.status_code == 200:
data = response.json()
issues.extend(data["issues"])

# Check if there are more issues to fetch
total = data["total"]
start_at += max_results
if start_at >= total:
break
else:
logger.error(f"Error fetching Jira issues: {response.text}")
raise Exception(f"Error fetching Jira issues: {response.text}")

return issues 
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 29, 2024

As an alternative variant you can use python library to work with Jira

It's  atlassian-python-api library, that can work with different products
https://atlassian-python-api.readthedocs.io/

 

from atlassian import Jira

jira = Jira(url = "https://jira_url/", token = jira_token)

 

Like abhay likes this
abhay October 29, 2024

Thanks

Like Evgenii likes this
0 votes
abhay October 29, 2024

Hey @Evgenii thanks for the reply
So you are using the requests library directly. I was asking with respect to jira pypi library in particular. Although I found a way to do it:

headers = {{
    "Accept": "application/json",
"Authorization": f'Bearer {{auth["accessToken"]}}',
}}

jira = JIRA(
server=auth["url"],
options={{'server': auth["url"]}}
)

jira._session.headers.update(headers)

Suggest an answer

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

Atlassian Community Events