Handling authentication and proxy servers with httplib2

末鹿安然 提交于 2019-12-10 23:33:54

问题


I'm attempting to test interactions with a Nexus server that requires authentication for the operations I intend to use, but I also need to handle an internal proxy server.

Based on my (limited) understanding I can add multiple handlers to the opener. However I'm still getting a 401 response. I've checked the username and password are valid. I'm not certain if cookies are required to do this and if so how they'd be included. Any suggestions?

baseUrl = 'server:8070/nexus-webapp-1.3.3/service/local'

params = {"[key]":"[value]"}

data = urllib.urlencode(params)

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

# Add the username and password as supplied
password_mgr.add_password(None, baseUrl, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)
proxy_support = urllib2.ProxyHandler({})

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

txheaders =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}

req = Request(protocol+url, data, txheaders)
handle = urlopen(req)

This is the resulting URLError's headers field:

>HTTPMessage: Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=B4BD05C9582F7B27495CBB675A339724; Path=/nexus-webapp-1.3.3
WWW-Authenticate: NxBASIC realm="Sonatype Nexus Repository Manager API"
Content-Type: text/html;charset=utf-8
Content-Length: 954
Date: Fri, 03 Jul 2009 17:38:42 GMT
Connection: close

Update It seems Nexus implement a custom version of Restlet's AuthenticationHelper. Thanks to Alex I knew what to look for.


回答1:


Can you show the full headers of the 401 response you're getting? Maybe it's not a basic auth request, maybe it's the proxy wanting its own authentication -- it's hard to guess without seeing said headers!

Edit: thanks for showing the headers (I reformatted them as "code" else they were unreadable).

As I suspected, it doesn't want "Basic", it wants some other (Nexus proprietary...?) "NxBASIC" authentication protocol -- I've never heard about it (I don't know anything about Nexus) and I imagine neither has the basic authentication handler you're using (even if NxBASIC somehow accepted plain Basic authentication, the handler, knowing only that it's a different protocol, would not offer such authentication).

So, first you need to research exactly what that NxBASIC thing is, and for that I suspect a SO question with the right tags might help. Then, depending on what you learn, comes the interesting issue of defining a handler for it...!-(



来源:https://stackoverflow.com/questions/1080179/handling-authentication-and-proxy-servers-with-httplib2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!