incubator-ambari-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mahadev Konar <maha...@hortonworks.com>
Subject Re: invoke REST calls from python-urllib2 gives error
Date Tue, 19 Mar 2013 20:32:13 GMT
Hi Subin,
 Unfortunately I am at Hadoop Summit Europe for this week. Ill try it
out over next week and let you know how it goes.

Please feel free to ping me on this if I do not respond in time :).

thanks
mahadev

On 3/16/13, Modeel, Subin <subin.modeel@hp.com> wrote:
> Hi
>
> I tried that. But it still didn't work.
> Where you able to get it working?
>
>>>> url = 'http://localhost:8080/api/v1/clusters/'
>>>>  import urllib2
>>>> password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
>>>> password_mgr.add_password(None,url ,"admin","admin")
>>>> handler = urllib2.HTTPBasicAuthHandler(password_mgr)
>>>> opener = urllib2.build_opener(handler)
>>>> urllib2.install_opener(opener)
>>>> 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
>
>
>
> Thanks
> Subin
>
>
>
>
>
>
> On Sat, Mar 16, 2013 at 9:30 PM, Mahadev Konar
> <mahadev@hortonworks.com<mailto:mahadev@hortonworks.com>> wrote:
> 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
> <subin.modeel@hp.com<mailto:subin.modeel@hp.com>> 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"
>>       }
>>     }
>>   ]
>> }
>
>


-- 
Mahadev Konar
Hortonworks Inc.
http://hortonworks.com/

Mime
View raw message