Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Yet Another 401 Error

Mohammad Hariri January 23, 2024

Hi,

I have been banging my head for a couple of days now and have not been able to access Jira through API using Atlassian or Jira python library. I have created a personal token which I use to pass to the server but no luck. I see Many posts here about 401 error but have not found a fix. Can anybody here help? 

Here is my code:

1) I installed library - pip install atlassian-python-api (same if I wanted to use Jira library)

2) Access the information:

from atlassian import Jira
import json
username = 'XXXX@XXXX.com'
api_token = "AAAAAAAAAABqze77sUyxKPCBfmZTWZ6"

jira = Jira(url=jira_url, username=username, password= api_token,cloud=True)
results = {}
boards= jira.get_issue(issue_id_or_key="COES-10")
3) I get:
"name": "HTTPError",
"message": "Unauthorized (401)",

2 answers

1 vote
Kai Becker
Community Champion
January 23, 2024

Hi @Mohammad Hariri 

welcome to the community and thank you for your question. 

Please try the following code and replace the parts in <> accordingly:

from atlassian import Jira
import json

jira_url = 'https://<sitename>.atlassian.net'
username = '<useraccount>'
api_token = "<token>"

jira = Jira(url=jira_url, username=username, password= api_token,cloud=True)
results = {}
boards=jira.get_issue("COES-10")

print(boards)

Problems with your code:

  • jira_url just needs to be the base url, no path required
  • get_issue method only requires the key in double quotes

 

@Sunny Ape just to clear things up. You referenced another python api , but not the one Mohammed is using. Might be confusing for others.

Sunny Ape
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2024

@Kai Becker 

OK, thanks. So many people just use the generic phrase "Atlassian Python Library" that's it's hard to tell which one they mean.

Like Kai Becker likes this
Mohammad Hariri January 24, 2024

This is what was installed as I mentioned in the original note. 

pip install atlassian-python-api
0 votes
Sunny Ape
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2024

Hello @Mohammad Hariri 

Firstly, stop banging your head on a table and start thinking like a programmer. If you have problem with your code, isolate it by trying another method.

Some other methods:

  1. Use a REST API test tool like Postman to confirm the request and username + token combination is valid.
  2. Use the native Python Request library to confirm the fault is not the other library.

Also, try reading the Atlassian Python Jira Module document more thoroughly, as I can see at least two major faults in your request.

  1. You have called jira.get_issue(key) but there is no such function, it is just plain jira.issue(key).
  2. Your URL indicates you are making a call to /rest/api/2/field , which is the Get Field endpoint, which means you should probably be using jira.issue_field_value(key, field)

All of this is described in the Manage Issues section of the library's documentation.

Lastly, as has been widely reported for many  years (if you do some Googling), that public Python library was built for Jira SERVER and calls only v2 endpoints, some of which are called differently in Jira CLOUD, so some of the functions simply don't work. You really, really should consider de-coupling your code's dependency on that library and use the native Python Requests library instead if you intend to interact with Jira Cloud exclusively.

Anyhow, have fun on your learning journey.

Mohammad Hariri January 25, 2024

Thank you for the guidance. I tried using pure requests exactly as defined here. I am still getting 401 error. Are there any settings I am missing on the backend token configuration? Not sure what can be done. All roads end in 401 error. 

Like Venkata r Kuppa likes this
Venkata r Kuppa May 10, 2024

Thank you @Mohammad Hariri  - Have been trying to connect Jira Cloud (corporate) from Python code.. never successful. at least 20+ combinations.

However, good old Excel VBA, was easier to connect

And we say 'Python' is most sophisticated .. haaa?

 

also adding @Sunny Ape 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events