pycontribs / jira

Python Jira library. Development chat available on https://matrix.to/#/#pycontribs:matrix.org
https://jira.readthedocs.io
BSD 2-Clause "Simplified" License
1.95k stars 862 forks source link

Jira token_auth not working #1897

Open MateusZ36 opened 1 week ago

MateusZ36 commented 1 week ago

Bug summary

I tried doing something like this:

jira_instance = JIRA(
    server='https://XXX.atlassian.net/',
    token_auth=access_token,
)

I'm getting the folowing error:

raise JIRAError(
jira.exceptions.JIRAError: JiraError HTTP 401 url: https://XXX.atlassian.net/rest/api/2/serverInfo
    text: Client must be authenticated to access this resource.

the access token is valid, normal requests to te API works.

Is there an existing issue for this?

Jira Instance type

Jira Cloud (Hosted by Atlassian)

Jira instance version

No response

jira-python version

3.8.0

Python Interpreter version

3.12

Which operating systems have you used?

Reproduction steps

# 1. Get an access token of your account

# 2. Try to use it to create an instance of JIRA
jira_instance = JIRA(
    server='https://XXX.atlassian.net/',
    token_auth=access_token,
)
# 3. "Client must be authenticated to access this resource." error shows up

Stack trace

Traceback (most recent call last):
  File "/path/to/your/project/jiratessts.py", line 20, in <module>
    jira_instance = JIRA(
                    ^^^^^
  File "/path/to/your/project/.venv/lib/python3.12/site-packages/jira/client.py", line 660, in __init__
    si = self.server_info()
         ^^^^^^^^^^^^^^^^^^
  File "/path/to/your/project/.venv/lib/python3.12/site-packages/jira/client.py", line 3599, in server_info
    j = self._get_json("serverInfo")
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/your/project/.venv/lib/python3.12/site-packages/jira/client.py", line 4358, in _get_json
    else self._session.get(url, params=params)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/your/project/.venv/lib/python3.12/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/your/project/.venv/lib/python3.12/site-packages/jira/resilientsession.py", line 247, in request
    elif raise_on_error(response, **processed_kwargs):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/your/project/.venv/lib/python3.12/site-packages/jira/resilientsession.py", line 72, in raise_on_error
    raise JIRAError(
jira.exceptions.JIRAError: JiraError HTTP 401 url: https://XXX.atlassian.net/rest/api/2/serverInfo
    text: Client must be authenticated to access this resource.

    response headers = {'Date': 'Wed, 18 Sep 2024 14:43:42 GMT', 'Content-Type': 'text/html;charset=UTF-8', 'Content-Length': '53', 'Server': 'AtlassianEdge', ...}
    response text = Client must be authenticated to access this resource.

Expected behaviour

It should authenticate and let me use the instance.

Additional Context

No response

dimitarOnGithub commented 1 week ago

Can you please try:

jira_instance = JIRA(
    server='https://XXX.atlassian.net/',
    basic_auth=(<username/email here>, <token here>),
)

and see if that works?

MateusZ36 commented 1 week ago

This way it doesn't give any error when instancing but later when trying to, for example: jira_instance.myself() or jira_instance.issue() it does

I'm getting the token trough OAuth 2.0 (3LO) apps, idk if this helps

MateusZ36 commented 4 days ago

Update: I was able to create an instance using this:

JIRA(
    server=f"https://api.atlassian.com/ex/jira/{cloud_id}",
    token_auth=access_token,
    validate=False
)

there are a few enpoints, like myself() that doesnt work because of the base URL but I can manage that