Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to use API token for REST calls in Python.

I'm currently intending to use the Jira API to pull data for more robust reporting. I am using Python 3.6, and the Python requests library.

Currently this works:

r = requests.get(jira_url_critquery, auth=('[username]','[password]'))

But I'd rather use the API token I've created. I've tried several different variations, but so far nothing has worked.

Here is an example of a url which works (with the username/password).

https://[our company account].atlassian.net/rest/api/2/search?jql=assignee=[my jira username]

 

Any thoughts?

4 answers

3 accepted

14 votes
Answer accepted

Hi Marvin, 

If you use the Jira Python library, just put the key where the password would go.

Example code (worked with the SAAS version of Jira, my user, my apikey)

#!/usr/bin/python3.6

# library modules
from jira import JIRA

user = 'me@here.com'
apikey = 'your0api0key0here'
server = 'https://SITE_NAME.atlassian.net'

options = {
'server': server
}

jira = JIRA(options, basic_auth=(user,apikey) )

ticket = 'KRP-11697'
issue = jira.issue(ticket)

summary = issue.fields.summary

print('ticket: ', ticket, summary)

Thank you for the answers, how can I get the users any sample python code to get user details who are in cloud Jira account?

Thanks. This worked perfectly.

I was struggling with outh but this solution is easier. Thank you! 

Can you authenticate for multiple servers in the same time? Something like

#!/usr/bin/python3.6

# library modules
from jira import JIRA

user = 'me@here.com'
apikey = 'your0api0key0here'
server1 = 'https://SITE_NAME1.atlassian.net'
server2 = 'https://SITE_NAME2.atlassian.net'


options = {
'server': [server1, sever2]
}

jira = JIRA(options, basic_auth=(user,apikey) )

(Obviously if the user has access to both)

Is this even possible? Or is there a workaround so it is achievable?

Like pradnya_ramtirthakar likes this

Can anyone help me out on how can I handle invalid username and password entered by the user. Thus, how can handle the calling of JIRA constructor using basic_auth but with wrong credentials ? ( not exception handling)

4 votes
Answer accepted

Standing on the shoulders of @Rob Echlin the Giant, here is my version:

import jira

jira = jira.JIRA('https://example.atlassian.net', basic_auth=('username@example.com', 'usernames_api_key'))

# print all of the project keys as an example
for project in jira.projects():
print(project.key)

Hello @John M 

it gives me this error : AttributeError: module 'jira' has no attribute 'JIRA'

 

Regards

Karim

1 vote
Answer accepted

Thank you sir. I shall give this a go.

Hi Marvin, 

If you use the Jira Python library, just put the key where the password would go.

 

Example code (worked with my server, user, apikey)

 

#!/usr/bin/python3.6

# library modules
from jira import JIRA

user = 'me@here.com'
apikey = 'your0api0key0here'
server = 'https://SITE_NAME.atlassian.net'

options = {
'server': server
}

jira = JIRA(options, basic_auth=(user,apikey) )

ticket = 'KRP-11697'
issue = jira.issue(ticket)

summary = issue.fields.summary

print('ticket: ', ticket, summary)

 

Like # people like this

Standing on the shoulders of giant Rob, here is my version:

import jira

jira = jira.JIRA('https://example.atlassian.net', basic_auth=('username@example.com', 'usernames_api_key'))

# print all of the project keys as an example
for project in jira.projects():
print(project.key)
Like Jason Fernandes likes this
0 votes
Andy Heinzer Atlassian Team Apr 05, 2019

@Rob Echlin and @John McGehee Thanks for posting your own solutions to this post.  If you could repost these as an answer, instead of a comment, we could then accept your answer for this question.  It would help other users that come across this thread to see more clearly other potential solutions.  

I could post your answers again myself, but it feels super disingenuous to claim your solution as my own, which is how it would appear if I just copy/pasted it.

Thanks

Andy

Thanks for the suggestion.
I created an answer from my comment.

Sincerely,
Rob

Like Andy Heinzer likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you