Reputation: 23
In Python, I used to be able to authenticate with JIRA over REST using a simple bit of code:
from jira import JIRA
my_JIRA_username = 'my_name'
my_JIRA_pass = 'my_password'
server = {'server': 'https://my_site.atlassian.net'}
jira = JIRA(server, basic_auth = (my_JIRA_username, my_JIRA_pass))
And then be able to do something simple like:
my_issue = jira.issue('MYISSUE-1')
print my_issue.key
But JIRA has changed things, and I am guessing no longer allows basic authentication like this via the REST API. Instead I get a 401 error.
I tired creating an API token, as suggested here: https://confluence.atlassian.com/cloud/api-tokens-938839638.html except I have NO idea where to use it (it doesn't work if I just use it as a password, like in the code above).
I looked into OAuth as an alternative but I cannot find any code that goes from point A to B that makes ANY sense to me.
Instead I find things like this: https://bitbucket.org/atlassianlabs/atlassian-oauth-examples/src/master/python/app.py
Which is pure gibberish to me. It can't be that complicated!
Can anyone help with a simple jira-python example of using something other than basic authentication that moves from the initialization to a simple REST call?
It would be most appreciated! I have months of work that are now useless merely because I can no longer authenticate.
Also, if you can explain how and when to use the API token that JIRA allows you to generate on their cloud instance, that would be appreciated to, as I don't understand that either!
Upvotes: 2
Views: 14987
Reputation: 88
Adding this solution here as the above does not work in newer versions with API Tokens. Hope it helps!
from jira import JIRA
# Basic Jira options
jira_options = {
"server": "jour jira url",
"verify": False # for self signed certs - otherwise the path
}
# create jira object with api token as auth method
jira_obj = JIRA(
options=jira_options,
token_auth="your api token here"
)
Upvotes: 0
Reputation: 1
This answer from atlassian helped me a lot:
#!/usr/bin/python3.6
# library modules
from jira import JIRA
user = '[email protected]'
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)
Upvotes: -1
Reputation: 711
Since using password is already deprecated in basic auth, API tokens are to be used in its place (as you've stumbled upon). In your code you need to use email
in place of username
and apiToken
instead of password. In your code it should be
# jira = JIRA(server, basic_auth = (my_JIRA_username, my_JIRA_pass))
jira = JIRA(server, basic_auth = (email, apiToken))
Try it out and do tell us how it goes.
Upvotes: 10