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.
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
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you're specifically using the jira Python library, bearer token authentication isn't exposed as directly as some other authentication methods. A common workaround is to initialize the client normally and then update the underlying session headers with your bearer token. This lets you continue using the library while authenticating with Authorization: Bearer <token> instead of basic authentication.
If you're starting a new integration, it's also worth looking at the atlassian-python-api library, which has built-in support for token-based authentication and can be simpler for certain use cases. Whichever approach you choose, make sure the token has the required scopes and permissions for the Jira REST endpoints you're calling. Atlassian generally recommends modern token- or OAuth-based authentication for new integrations rather than older authentication methods.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
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.