How do I access the hosted Jira API via python?

red888 August 5, 2016

Using the JIRA python module this example works:

 

from jira import JIRA
jira = JIRA('https://jira.atlassian.com')
issue = jira.issue('JRA-9')
print(issue.fields.project.key) # 'JRA'
print(issue.fields.issuetype.name) # 'New Feature'
print(issue.fields.reporter.displayName) # 'Mike Cannon-Brookes [Atlassian]'

But I can get authentication working.

For example how do I query an issue named ISE-4 in a project name MYPROJECT

I tried authed_jira = JIRA(basic_auth=('myuser', 'mypass')) but this does not seem to work.

Does the hosted JIRA API support basic authentication?

4 answers

1 accepted

11 votes
Answer accepted
Jeff
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 5, 2016

EDIT: Standard basic authentication is now deprecated in Jira from June 2019.

An API key is required for basic authentication which now replaces the 'password' requirement. API key's can be 
generated from:  https://confluence.atlassian.com/cloud/api-tokens-938839638.html.

You should use the following format:

jira = JIRA(
basic_auth=(un, pwd),
options={
'server': server
}
)

You need to pass in the server details as a connection option when you are specifying basic_auth.

in the example above, un pwd and server are all string variables.

red888 August 5, 2016

When I try that I get a syntax error:

 

from jira import JIRA
 
#jira = JIRA('https://jira.atlassian.com')
jira = JIRA(basic_auth=('user', 'password123!'), options={'https://myaccount.atlassian.net'})
issue = jira.issue('blah-12')
print(issue.fields.project.key) 
print(issue.fields.issuetype.name) 
print(issue.fields.reporter.displayName)

 

I Get this error:
ValueError: dictionary update sequence element #0 has length 29; 2 is required

Jeff
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 5, 2016

You haven't setup your dictionary correctly in the example above, you just specified the url as a key with no value, instead of a key/value pair.

Options should be:

options = {'server': 'https://myaccount.atlassian.net'}
Like # people like this
red888 August 6, 2016

Ah sorry new to python, looks like she workin now thanks a bunch.

7 votes
Harihar Pai December 28, 2017

This works ....

from jira import JIRA

def main():

   options = {'server': jiraURL}
   jira = JIRA(options, basic_auth=(jiraUserName, jiraPassword))
   issue = jira.issue('ESS-138581')

   print issue.fields.project.key
   print issue.fields.issuetype.name
   print issue.fields.reporter.displayName
   print issue.fields.summary
   print issue.fields.project.id

if __name__== "__main__" :
     main()

 

If you get handshake error [SSLV3_ALERT_HANDSHAKE_FAILURE]. Kindly install the following modules in python

  • pyOpenSSL
  • ndg-httpsclient
  • pyasn1
Santhosh Kumar May 28, 2019

Great !! this sample of code works..

Ruchit May 30, 2019

While trying above code @Harihar Pai I am getting below error:

WARNING:root:Got recoverable error from GET https://jira.company name/rest/api/2/serverInfo, will retry [1/3] in 6.687988879187072s. Err: 401

Can anyone explain to me why I am getting this error

AZhylkaidarov June 27, 2019

Same here, on-premise jira is giving this error out of nowhere 

rgronber July 18, 2019

@Ruchit @Aibek Prenov When I was using this module with on prem JIRA, I could not use my real username to authenticate, but I had to use the JIRA assigned user key. In my organization, the key is of the form 'cc######'. I am not sure where you can determine your user key. For me, if I attempt to login to the web UI with my regular username, but the wrong password, when I am redirected back to the login page, my real username has been replaced with my user key.

Aditya_Sharma April 18, 2020

Hi , When i use this code, I am getting SSL erro along wth No Certificate Error?

5 votes
Luke Gibbins August 20, 2019

Standard basic authentication is now deprecated in Jira from June 2019.

An API key is required for basic authentication which now replaces the 'password' requirement. API key's can be 
generated from:  https://confluence.atlassian.com/cloud/api-tokens-938839638.html.

Once the key has been obtained, replace your current Jira object with the following:
jira = JIRA(basic_auth=("enter username", "enter API key"), options={'server': "enter server"}).

Dylan Brophy February 9, 2021

This should be in the documentation...

Dylan Brophy February 9, 2021

Worked. :D

2 votes
IPTV KODI July 4, 2018

Python | Step by Step how to access the hosted Jira API via python
[http://thepythoncoding.blogspot.com/2018/07/python-step-by-step-how-to-access.html]

Kais Tounsi July 17, 2019

you should use the following format:

jira = JIRA(basic_auth=(un, pwd), options={'server': server})
Like Ricardo Cerceau likes this
Kais Tounsi July 17, 2019
from jira import JIRA
jira = jira = JIRA(basic_auth=(un, pwd), options={'server': server})
Like Ricardo Cerceau likes this

Suggest an answer

Log in or Sign up to answer