Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Get token information to print out in python program using Jira package

E_ S_ Hansen June 28, 2023

Hello,

I am writing a python program in which I am authenticating myself to Jira using

jira = JIRA(server="https://jira-pd.mycompany.biz", token_auth="{mytoken}")

To keep track on tokens I want to print out information about the token used, specifically "name" and "expiringAt" which can be retrieved from "rest/pat/latest/tokens". I tried to write a function doing this:

def get_token_info():
url = "https://jira-pd.mycompany.biz/rest/pat/latest/tokens"
payload = {"name": "value1", "expiringAt": "value2"}
r = requests.get(url, params=payload)
print(r.text)

This function returns a 401 status saying "Client must be authenticated to access this resource". I suspect that the get-function does not align well with the Jira package, as the package seems to be constructed to handle Jira issues and doesn't incorporate get().

Worth mentioning is that I have tried other methods of authentication (HTTPBasicAuth and OAuth2) which I didn't get to work. My code also involves retrieving issue-data which has been easily handled using the Jira package, so I would prefer if I could keep my current authentication method.

How can I go about this issue?

1 answer

0 votes
Leon Pavesic
Contributor
June 28, 2023

In order to retrieve token information from Jira using the Jira Python package, you need to use the built-in methods provided by the package instead of directly making requests with the `requests` library. The Jira Python package offers an interface to interact with Jira's REST API.

To retrieve token information using the Jira Python package, you can use the `get_all_tokens` method available for the Jira instance. Here's an example:


from jira import JIRA

# Authenticate to Jira
jira = JIRA(server="https://jira-pd.mycompany.biz", token_auth="{mytoken}")

def get_token_info():
# Get all tokens
tokens = jira.get_all_tokens()

# Iterate through tokens and print information
for token in tokens:
name = token.name
expiring_at = token.expiringAt
print(f"Token Name: {name}, Expiring At: {expiring_at}")

# Call the function
get_token_info()
```

This code uses the `get_all_tokens` method of the Jira instance to retrieve all tokens associated with the authenticated user. Then, it iterates through the tokens and prints the name and expiration information.

Make sure to replace `"{mytoken}"` with your actual token value when authenticating to Jira. Also, ensure that the token you provide has the necessary permissions to access token information.

Using the Jira Python package's built-in methods is the recommended approach to interact with Jira's API and retrieve token information, as it handles authentication and other necessary configurations for you.

Kindest regards

Leon

E_ S_ Hansen June 28, 2023

Thank you for the very kind reply!

There seems to be no function "get_all_tokens()'' in my Jira package. Do you know where I can find the raw code for that function or how I can access it?

Suggest an answer

Log in or Sign up to answer