pythonteamcity

HTTP Error 401: Unauthorized using urllib.request.urlopen


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 *


Solution

  • 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