2

I am trying to connect to a REST resource and retrieve the data using Python script (Python 3.2.3). When I run the script I am getting error as HTTP Error 401: Unauthorized. Please note that I am able to access the given REST resource using REST client using Basic Authentication. In the REST Client I have specified the hostname, user and password details (realm is not required). Below is the code and complete error. Your help is very much appreciated.

Code:

import urllib.request

# set up authentication info
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm=None,
                       uri=r'http://hostname/',
                       user='administrator',
                       passwd='administrator')
opener =  urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
res = opener.open(r'http://hostname:9004/apollo-api/nodes')
nodes = res.read()

Error

Traceback (most recent call last):
File "C:\Python32\scripts\get-nodes.py", line 12, in <module>
    res = opener.open(r'http://tolowa.wysdm.lab.emc.com:9004/apollo-api/nodes')
File "C:\Python32\lib\urllib\request.py", line 375, in open
   response = meth(req, response)
File "C:\Python32\lib\urllib\request.py", line 487, in http_response
   'http', request, response, code, msg, hdrs)
File "C:\Python32\lib\urllib\request.py", line 413, in error
   return self._call_chain(*args)
File "C:\Python32\lib\urllib\request.py", line 347, in _call_chain
   result = func(*args)
File "C:\Python32\lib\urllib\request.py", line 495, in http_error_default
   raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized
1

2 Answers 2

4

Try to give the correct realm name. You can find this out for example when opening the page in a browser - the password prompt should display the name.

Sign up to request clarification or add additional context in comments.

2 Comments

This, or use the HTTPPasswordMgrWithDefaultRealm password manager instead.
Thanks jOnes and @Martijn Pieters for helping me in resolving this issue.
3

You can also read the realm by catching the exception that was raised:

import urllib.error
import urllib.request

# set up authentication info
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm=None,
                       uri=r'http://hostname/',
                       user='administrator',
                       passwd='administrator')
opener =  urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
try:
    res = opener.open(r'http://hostname:9004/apollo-api/nodes')
    nodes = res.read()
except urllib.error.HTTPError as e:
    print(e.headers['www-authenticate'])

You should get the following output:

Basic realm="The realm you are after"

Read the realm from above and set it in your add_password method and it should be good to go.

1 Comment

Thanks @Thierry Lam for helping me in resolving this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.