HTTP Error 401: Unauthorized using urllib.request.urlopen

John

I'm using urllib.request in python to try and download some build information from Teamcity. This request used to work without username and password, however a recent security change means I must use a username and password. So I have changed tried each of the two solutions below:

Attempt 1)

url = 'http://<domain>/httpAuth/app/rest/buildTypes/<buildlabel>/builds/running:false?count=1&start=0'

# create a password manager
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
top_level_url = "http://<domain>/httpAuth/app/rest/buildTypes/id:<buildlabel>/builds/running:false?count=1&start=0"
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib.request.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener(handler)

# use the opener to fetch a URL
opener.open(url)

Attempt 2

url = 'http://<username>:<password>@<domain>/httpAuth/app/rest/buildTypes/id:buildlabel/builds/running:false?count=1&start=0'
rest_api = urllib.request.urlopen(url)

Both of these return "HTTP Error 401: Unauthorized". However if I was to print 'url' and copy this output into a browser the link works perfectly. But when used through python I get the above error.

I use something very similar in another Perl script and this works perfectly also.

* SOLVED BELOW *

John

Solved this using.

credentials(url, username, password)
rest_api = urllib2.urlopen(url)
latest_build_info = rest_api.read()
latest_build_info = latest_build_info.decode("UTF-8")
# Then parse this xml for the information I want. 

def credentials(self, url, username, password):
    p = urllib2.HTTPPasswordMgrWithDefaultRealm()
    p.add_password(None, url, username, password)
    handler = urllib2.HTTPBasicAuthHandler(p)
    opener = urllib2.build_opener(handler)
    urllib2.install_opener(opener)

As a side note, I then want to download a file..

credentials(url, username, password)
urllib2.urlretrieve(url, downloaded_file)

Where Url is:

http://<teamcityServer>/repository/download/<build Label>/<BuildID>:id/Filename.zip

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

HTTP 401 Unauthorized error occurs in Spring Boot test

urllib HTTPS request: <urlopen error unknown url type: https>

curl 401 Unauthorized error

HTTPError: HTTP Error 401: Unauthorized for sendgrid integration with python

Django Rest Framework: HTTP 401 Unauthorized error

HTTP response '401: Unauthorized' using NTLM with Wildfly

How to fix HTTP error in Python 3 using urlopen with urllib

HTTP Error 403 when using urllib urlopen in python to pull Tesla charger information

React and Redux: 401 Unauthorized Error POST API Request

I tried to call a post request using PHP code, but I got 401 Unauthorized error.

Python Linkedin API OAuth2 HTTP Error 401: Unauthorized

Twitter API returned a 401 (Unauthorized), An error occurred processing your request

Rails - RestClient Unauthorized (401) Error, user is not recognized for POST request

Solr HTTP error: Unauthorized (401)

cloud endpoints return error 401 unauthorized request

"HTTP Error 401: Unauthorized" when querying youtube api for playlist with python

HTTP/1.1 401 Unauthorized Error on curl request - heroku

getting HTTP Status 401 - unauthorized user error for ebayclassifeids Api

catch a 401 Unauthorized http response using jquery ajax

urllib.request fails on urlopen of unicode string

HTTP Error when using urllib.request

How to make a urllib2 POST request without using urlopen?

401 UnAuthorized - This request requires HTTP authentication - Payara/Glassfish

Python 3.6.2 url.request.urlopen() urllib.error.HTTPError: HTTP Error 403: Forbidden

urllib.error.HTTPError: HTTP Error 404: Not Found when using request.urlopen()

python: urllib.request.urlopen() HTTP Error 308 Permanent redirect

Error while installing packages using yarn or npm, `Request failed \"401 Unauthorized`

using a varible in "urllib.request.urlopen" throws an error

401 Unauthorized error Firebase using Angular