Subin,
 Looks like you might have to install the handler:

 Here's the snippet from python docs:

========
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
# If we knew the realm, we could use it instead of None.
top_level_url = "http://example.com/foo/";
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

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

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

# Install the opener.
# Now all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)

========

thanks
mahadev


On Fri, Mar 15, 2013 at 2:50 AM, Modeel, Subin <[email protected]> wrote:
> Hi
> I was trying to invoke the rest call from python-urllib2 .I see that It fails 
> with 403 error.I feel its because there is no realm value.
> I am unable to find the WWW-Authenticate: value to find the realm value.
> Can you please help me with this.
>
>
> Thanks
>
>>>> import urllib2
>>>> url = 'http://localhost:8080/api/v1/clusters/'
>>>> password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
>>>> handler = urllib2.HTTPBasicAuthHandler(password_mgr)
>>>> password_mgr.add_password(None,'http://localhost:8080','admin', 'admin')
>
>>>> opener.open('http://localhost:8080/api/v1/clusters/')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib64/python2.6/urllib2.py", line 397, in open
>     response = meth(req, response)
>   File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
>     'http', request, response, code, msg, hdrs)
>   File "/usr/lib64/python2.6/urllib2.py", line 435, in error
>     return self._call_chain(*args)
>   File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
>     result = func(*args)
>   File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default
>     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
> urllib2.HTTPError: HTTP Error 403: Full authentication is required to access 
> this resource
>
>
>
> I am able to use the pycurl library to get the response.
>
>
>>>> import pycurl, json
>>>> url = 'http://localhost:8080/api/v1/clusters/'
>>>> c = pycurl.Curl()
>>>> c.setopt(pycurl.URL, url)
>>>> c.setopt(pycurl.USERPWD,'admin')
>>>> c.setopt(pycurl.USERPWD,'admin:admin')
>>>> c.perform()
> {
>   "href" : "http://localhost:8080/api/v1/clusters/";,
>   "items" : [
>     {
>       "href" : "http://localhost:8080/api/v1/clusters/test1";,
>       "Clusters" : {
>         "cluster_name" : "test1",
>         "version" : "HDP-1.2.0"
>       }
>     }
>   ]
> }

Reply via email to