princenyeche/jiraone

How-to: Using Jira api token to log in

manojs888 opened this issue · 8 comments

have been trying to get this working in python, but have not been able to work out how to login using email address and API token (created from Jira personal profile) ?

I have tried the below and it gives me:

Traceback (most recent call last):
File "E:\Downloads\from jiraone import LOGIN.py", line 12, in
PROJECT.change_log(jql=jql)
File "C:\Users\msm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\jiraone\reporting.py", line 827, in change_log
raise JiraOneErrors("login", "Authentication failed. Please check your credentials.")
jiraone.exceptions.JiraOneErrors: <JiraOneError: Authentication failed. Please check your credentials.>

from jiraone import LOGIN, PROJECT

user = ""
password = ""
link = ""
LOGIN(user=user, password=password, url=link)

if name == 'main':
# the output of the file would be absolute to the directory where this python file is being executed from
jql = "project in (PYT) ORDER BY Rank DESC" # A valid JQL query
PROJECT.change_log(jql=jql)

@manojs888 are you connecting to a Jira cloud or Jira Server/DC instance?

You need to ensure that you're generating your token from here and use it within the script.

from jiraone import LOGIN, PROJECT

user = "emailaddress"
password = "token"
link = "https://yourinstance.atlassian.net"
LOGIN(user=user, password=password, url=link)

if __name__ == '__main__':
    # the output of the file would be absolute to the directory where this python file is being executed from
    jql = "project in (PYT) ORDER BY Rank DESC"  # A valid JQL query
    PROJECT.change_log(jql=jql)

Im facing the same issue on datacenter. When executing:

LOGIN.api = True
LOGIN(password = jira_token ,url = url)
LOGIN.get(endpoint.myself())

everything seems to work: <Response [200]>

However, when using the Module time_in_status I get a JSONDecoderError:

    353     obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I assume that "LOGIN.api = True" is only for Jira cloud? However, when setting api to False I cannot even reach the endpoint:<Response [401]>

Am I missing anything? Or do you happen to know a workaround?

@Felix313 The LOGIN.api could be True or False for cloud but for DC or Server it must be False. Based on your auth method, I believe you're using a PAT on DC. Then you need to use the token session instead.

from jiraone import LOGIN, endpoint

# previous statement
url = "https://yourserver.datacenter.com"
token = "GHxxxxxPPPxx"
# First assign a base_url to the attribute below
LOGIN.base_url = url
LOGIN.api = False
# You need to pass the token variable to a keyword argument called `sess`
LOGIN.token_session(sess=token)
output = LOGIN.get(endpoint.myself())
print(output.json())

The above uses a Bearer authorization header instead which should be the proper way to authenticate with a PAT. Let me know if that works for you?

Works perfectly, thank you so much for your help! I tried using token_session but passed token=token instead of sess=token.

That's interesting, that creates a basic authentication rather than bearer authentication but if that works for you it's okay.