Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Getting 401 unauthorized error python after trying everything

Vibhor Malik July 18, 2021

Hi All, please help. I tried everything still getting 401 unauthorized error in python. Below is my code.

url = 'https://jira.mycompanyname.org/rest/api/2/issue/XYZ-85'
server = 'https://jira.mycompanyname.org'

headers = {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization': 'Basic [base64 encoded string of my office email id:auth token]'
}
options = {
'server':sever
}

query = {
'jql':'project=XYZ'
}

response = requests.get(url,headers=headers,params=query)
print(response.text)



Please help. I am exhausted trying every page on this community and you tube videos, nothing is working :(

 

PS can this be because our company uses OKTA to login into apps?

however NOTE - when I am opening below URL in my browser its showing all information

'https://jira.mycompanyname.org/rest/api/2/issue/XYZ-85

 

Many thanks

 

2 answers

0 votes
David Bakkers
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.
July 18, 2021

Hello @Vibhor Malik 

The reason why the GET works in your browser is that it's already passed through the OKTA 2FA sequence when you logged into the Jira web GUI and has a session cookie, so it's pre-authenticated :)

I'm currently working in an environment that uses OKTA for 2FA login to Jira for their users, but to work around this to access their Jira Cloud REST API without having to enable and use OAuth, I got the company's firewall admins to add a whitelist rule to allow the connection to the REST API over port 43 as an exception. Once that was done, everything was fine and I was able to use the normal Key / Token and bundle it into a Basic auth session over HTTPS.

Most companies are understanding enough to allow these sorts of small, special purpose exception cases to OKTA, provided they know why you're doing it and trust you.

If the company won't allow that sort of OKTA bypass, then they'll have no choice but to enable the application linking for you, and you'll have build the 3LO sequence into your python.

Vibhor Malik July 19, 2021

Hi @David Bakkers Many thanks for the information. This is really helpful.

One quick question - as I am able to get all info in the browser, is there any way to retain that information? OR I was reading somewhere that we can use JSessionID  or session cookie to read info from browser in any tool.

Is there any way I could browser info because I only need to GET info and not POST it and how to do it in python?

Thanks.

Vibhor

David Bakkers
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.
July 19, 2021

Hello @Vibhor Malik 

If you want to keep the JSON output from the REST API that is shown on the browser window, it's just text so you can either copy and paste it into a text file, or use the browser's Save function to keep it. Raw JSON isn't very readable, so it's a good idea to use a browser side add-on that parses the JSON and converts it back to human readable text. Firefox has a built-in JSON parser where Chromium based browsers need an extension.

As for for some other browser side app or tool re-using the same session cookie that was created after logging into Jira, it's not likely; the cookie is unique and specific to that login session only and cannot be used for anything else. Modern web browsers do session sandboxing to stop apps in one window from accessing the cookies of other apps in other windows as a deliberate security feature.

Vibhor Malik July 19, 2021

Thanks @David Bakkers 

However manual copy pasting would be a tedious task as I have to go through issue 1 to issue 2500 till date and counting more in future

so I may have to work on some loop in python and use that info which is displayed in browser. Is there any way we can do this?

 

As far as reading Json is concerned I am only interested in task close date so I can create a method or RegEx to pick that info from it. 

 

Thanks again

Vibhor

Vibhor Malik July 19, 2021

Sorry not the issue closed date - because that was available in excel download directly from Jira. I am interested in only a date when the issue was changed to one XYZ specific status, which sadly is not available in excel/CSV/XML download form Jira.

David Bakkers
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.
July 19, 2021

Hello @Vibhor Malik 

Yes, doing all the work in Python is of course much, much faster than copying and pasting requests one at a time into a browser window.... but since you asked if it was possible, I just advised that it was.

Look through the REST API documentation. For every single endpoint it provides an example of how to construct a request in Python.

As for how to build Python scripts to then display the results in a browser window... that is a huge subject that is beyond the scope of this discussion.

Vibhor Malik July 19, 2021

Sorry @David Bakkers , didnt mean to offend. I only wanted to clarify my ask. :) 

Also I am actually asking that - 

is there a way I can loop the issue number in URL,  and when URL hits and it shows Json info in the browser, I pick that one info I need and save somewhere (all this using python)

 

Please suggest. You really had been of great help till now. :) 

David Bakkers
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.
July 19, 2021

Hello @Vibhor Malik 

To me it sounds like you're asking "Can you please write some Python code for me that does XYZ"

If that is the case, I would suggest there are plenty of free online tutorials that will guide you through how to write in Python, and literally thousands of examples of using Python to interact with REST APIs in general, or Jira specifically.

I wish you the best of luck on your learning adventure.

Vibhor Malik July 19, 2021

Hi @David Bakkers 

 

No I actually meant if that is feasible? and if yes - if you can head me to right direction  like a link or video which explains it how?

because I do know little bit of scraping and when trying with Selenium and BeautifulSoup (after opening the URL) it still gives me 401 unauthorized error.

 

Thanks

Vibhor

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2021

Okta doesn't support basic authentication, you'll need to use Oauth, Andy gives us a good guide to this at https://community.atlassian.com/t5/Jira-questions/JIRA-Rest-api-Access-with-OKTA/qaq-p/1186836

Vibhor Malik July 18, 2021

Hi @Nic Brough -Adaptavist- Thanks for the reply. 

I checked into this - however the steps to use Oauth need to create an application link in Jira. (here) - however my company doesnt give me access to go  to Jira settings, so I cant get past by this step.

Is there any way I can setup or you can help set this up?

If not possible - I only need data from Jira. and I can see that data in lot of text format when I paste this link in browser 'https://jira.mycompanyname.org/rest/api/2/issue/XYZ-85

Is there a way to extract that data from browser by scraping or any other way to python?

 

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events