Not an admin and using 2 -factor authentication - Failing with 401 error when try to authenticate

Hi,
I actually want to access the comments of all the tickets that I filtered based on a JQL query. I exported the tickets to an excel sheet, but the comments don’t get exported. So I tried to do it programmatically using Python and the JIRA rest API but its failing with 401 at the authentication level itself. I used username and API token. I am wondering if its because I do not have admin rights on my user and also, I use 2 factor authentication to login to my company’s Kazan JIRA portal. This is the authentication code part which is failing

# JIRA base URL

JIRA_BASE_URL = ‘https://jira.abc-solutions.com

JIRA_USERNAME = ‘MY LOGIN EMAIL ID’

JIRA_TOKEN = ‘THE API TOKEN THAT I GENERATED’

response = requests.get(

  • f’{JIRA_BASE_URL}/rest/api/3/search’,*
  • auth=HTTPBasicAuth(JIRA_USERNAME, JIRA_TOKEN)*
    )
    print(response.status_code)

I would appreciate any help in this direction. If there is any other way through which I can extract comments and worklog for the tickets, then I would appreciate that too. Thanks a lot.

Hello Sudha,
maybe this few steps will help you resolve your issue:

  1. Use OAuth 2.0 Authentication: Since you’re using 2FA, basic authentication might not work. Instead, consider using OAuth 2.0 for authentication2. This method is more secure and better suited for applications requiring higher security.
  2. Generate an Access Token: You’ll need to generate an access token using OAuth 2.0. Here’s a basic example of how to do this:
import requests

JIRA_BASE_URL = 'https://jira.abc-solutions.com'
CLIENT_ID = 'your_client_id'
CLIENT_SECRET = 'your_client_secret'
REDIRECT_URI = 'your_redirect_uri'
AUTH_URL = f'{JIRA_BASE_URL}/oauth/token'

payload = {
    'grant_type': 'client_credentials',
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET
}

response = requests.post(AUTH_URL, data=payload)
access_token = response.json().get('access_token')
  1. Use the Access Token: Once you have the access token, use it to authenticate your API requests:
headers = {
    'Authorization': f'Bearer {access_token}'
}

response = requests.get(f'{JIRA_BASE_URL}/rest/api/3/search', headers=headers)
print(response.status_code)
  1. Check Permissions: Ensure that your user account has the necessary permissions to access the comments and worklogs. If you don’t have admin rights, you might need to request the appropriate permissions from your Jira administrator.
  2. Extract Comments and Worklogs: Once authenticated, you can use the REST API to extract comments and worklogs. Here’s an example of how to get comments for a specific issue:
issue_key = 'your_issue_key'
comments_url = f'{JIRA_BASE_URL}/rest/api/3/issue/{issue_key}/comments'
response = requests.get(comments_url, headers=headers)
comments = response.json()
print

Best regards,
Daniel