I am trying to write some python code to get all tasks assigned to me on Jira for a specific time period. So far my code looks something like this:
# Jira Cloud instance URL
JIRA_URL = "https://xxxxxx.atlassian.net"
# API token and email for authentication (stored as environment variables)
API_TOKEN = getenv("JIRA_API_TOKEN")
EMAIL = getenv("USER_EMAIL")
JIRA_ACCT_ID = getenv("JIRA_ACCT_ID")
# Calculate today's date and last year's date dynamically
today = datetime.now().strftime("%Y-%m-%d")
one_year_ago = (datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
# JQL query to fetch all issues updated in the last year assigned to current user
raw_jql_query = f"assignee={JIRA_ACCT_ID} AND updated >= \"{one_year_ago}\" AND updated <= \"{today}\""
encoded_jql_query = quote(raw_jql_query)
# Headers for authentication
HEADERS = {
"Authorization": f"Basic {EMAIL}:{API_TOKEN}",
"Content-Type": "application/json"
}
# Function to fetch issues
def fetch_issues():
url = f"{JIRA_URL}/rest/api/3/search?jql={encoded_jql_query}&maxResults=100"
response = requests.get(url, headers=HEADERS)
The credentials are correct and I do get a response:
Status Code: 200
Response Text: {"startAt":0,"maxResults":100,"total":0,"issues":[]}
When I physically go to the URL generated above, There is a lot of information under `issues` but the response gives me an empty list (as seen above). Is it a security reasoned block or am I missing something?
Thank you!
Hi Harshad. Welcome to Atlassian Community!
It is not a permission issue, otherwise you would not be able to see the results in the browser.
I was able to reproduce your code in my envirorment and found that the issue is the authentication. The e-mail and token should be encoded in base64 before joining with the "Basic " string.
You should use like this:
import requests
import base64
from datetime import datetime, timedelta
from urllib.parse import quote
from os import getenv
JIRA_URL = "https://gsk-onyx.atlassian.net"
API_TOKEN = getenv("JIRA_API_TOKEN")
EMAIL = getenv("USER_EMAIL")
JIRA_ACCT_ID = getenv("JIRA_ACCT_ID")
today = datetime.now().strftime("%Y-%m-%d")
one_year_ago = (datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
raw_jql_query = f'assignee="{JIRA_ACCT_ID}" AND updated >= "{one_year_ago}" AND updated <= "{today}"'
encoded_jql_query = quote(raw_jql_query)
auth_str = f"{EMAIL}:{API_TOKEN}"
b64_auth_str = base64.b64encode(auth_str.encode()).decode()
HEADERS = {
"Authorization": f"Basic {b64_auth_str}",
"Content-Type": "application/json"
}
def fetch_issues():
url = f"{JIRA_URL}/rest/api/3/search?jql={encoded_jql_query}&maxResults=100"
response = requests.get(url, headers=HEADERS)
return response.json()
# Example use
issues = fetch_issues()
print(issues)
Thank you so much! That was it!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm glad to help.
If you can accept my comment as the anwser, I would appreciate it.
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.