princenyeche / jiraone

A REST API Implementation to Jira Cloud APIs for creating reports and for performing other Jira queries.
https://jiraone.readthedocs.io
MIT License
25 stars 11 forks source link

How-to: Using Jira api token to log in #104

Closed manojs888 closed 1 year ago

manojs888 commented 1 year ago

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:

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)
princenyeche commented 1 year ago

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

manojs888 commented 1 year ago

Jira cloud please, thx

On Mon, 16 Jan 2023, 22:44 Prince, @.***> wrote:

@manojs888 https://github.com/manojs888 are you connecting to a Jira cloud or Jira Server/DC instance?

— Reply to this email directly, view it on GitHub https://github.com/princenyeche/jiraone/issues/104#issuecomment-1384638441, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALUAPCJBH4LT7ISI5Y5R7T3WSXFLVANCNFSM6AAAAAAT5GKCU4 . You are receiving this because you were mentioned.Message ID: @.***>

princenyeche commented 1 year ago

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)
manojs888 commented 1 year ago

Thank you - all good now

Assistance much appreciated

Kind Regards

On Tue, 17 Jan 2023 at 18:16, Prince @.***> wrote:

You need to ensure that you're generating your token from here https://id.atlassian.com/manage-profile/security/api-tokens 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)

— Reply to this email directly, view it on GitHub https://github.com/princenyeche/jiraone/issues/104#issuecomment-1385837118, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALUAPCK6XEWTZK62ZFAI3Z3WS3OX7ANCNFSM6AAAAAAT5GKCU4 . You are receiving this because you were mentioned.Message ID: @.***>

Felix313 commented 1 year ago

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_instatus 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?

princenyeche commented 1 year ago

@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?

Felix313 commented 1 year ago

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

princenyeche commented 1 year ago

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