Python urllib2基本身份验证问题

Python urllib2基本身份验证问题,python,authentication,urllib2,Python,Authentication,Urllib2,更新:根据Lee的评论,我决定将代码压缩为一个非常简单的脚本,并从命令行运行它: import urllib2 import sys username = sys.argv[1] password = sys.argv[2] url = sys.argv[3] print("calling %s with %s:%s\n" % (url, username, password)) passman = urllib2.HTTPPasswordMgrWithDefaultRealm() pass

更新:根据Lee的评论,我决定将代码压缩为一个非常简单的脚本,并从命令行运行它:

import urllib2
import sys

username = sys.argv[1]
password = sys.argv[2]
url = sys.argv[3]
print("calling %s with %s:%s\n" % (url, username, password))

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))

req = urllib2.Request(url)
f = urllib2.urlopen(req)
data = f.read()
print(data)
不幸的是,它仍然不会生成
授权
头(每个Wireshark):(

我在通过urllib2发送基本身份验证时遇到问题。我查看了,并按照示例进行了操作。我的代码:

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "api.foursquare.com", username, password)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))

req = urllib2.Request("http://api.foursquare.com/v1/user")    
f = urllib2.urlopen(req)
data = f.read()
我通过wireshark在电线上看到以下内容:

GET /v1/user HTTP/1.1
Host: api.foursquare.com
Connection: close
Accept-Encoding: gzip
User-Agent: Python-urllib/2.5 
您可以看到,与通过curl:
curl-u user:password发送请求时相比,授权没有发送http://api.foursquare.com/v1/user

GET /v1/user HTTP/1.1
Authorization: Basic =SNIP=
User-Agent: curl/7.19.4 (universal-apple-darwin10.0) libcurl/7.19.4 OpenSSL/0.9.8k zlib/1.2.3
Host: api.foursquare.com
Accept: */*
出于某种原因,我的代码似乎没有发送身份验证-有人看到我缺少什么吗

谢谢


-simon

第二个参数必须是URI,而不是域名

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "http://api.foursquare.com/", username, password)

问题可能是,根据HTTP标准,Python库首先发送一个未经验证的请求,然后只有在401重试后才发送正确的凭据。如果Foursquare服务器不执行“完全标准身份验证”,则库将无法工作

尝试使用标头进行身份验证:

import urllib2, base64

request = urllib2.Request("http://api.foursquare.com/v1/user")
base64string = base64.b64encode('%s:%s' % (username, password))
request.add_header("Authorization", "Basic %s" % base64string)   
result = urllib2.urlopen(request)
遇到与您相同的问题,并从此线程找到解决方案:

(复制粘贴/改编自)

首先,您可以将
urllib2.BaseHandler
urllib2.HTTPBasicAuthHandler
子类化,并实现
http\u请求
,以便每个请求都具有适当的
授权

import urllib2
import base64

class PreemptiveBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
    '''Preemptive basic auth.

    Instead of waiting for a 403 to then retry with the credentials,
    send the credentials if the url is handled by the password manager.
    Note: please use realm=None when calling add_password.'''
    def http_request(self, req):
        url = req.get_full_url()
        realm = None
        # this is very similar to the code from retry_http_basic_auth()
        # but returns a request object.
        user, pw = self.passwd.find_user_password(realm, url)
        if pw:
            raw = "%s:%s" % (user, pw)
            auth = 'Basic %s' % base64.b64encode(raw).strip()
            req.add_unredirected_header(self.auth_header, auth)
        return req

    https_request = http_request
然后,如果您像我一样懒惰,请全局安装处理程序

api_url = "http://api.foursquare.com/"
api_username = "johndoe"
api_password = "some-cryptic-value"

auth_handler = PreemptiveBasicAuthHandler()
auth_handler.add_password(
    realm=None, # default realm.
    uri=api_url,
    user=api_username,
    passwd=api_password)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

我建议当前的解决方案是使用我的软件包,它很好地解决了这个问题(我使用标准库。

以下是我在尝试访问MailChimp的API时遇到的类似问题的处理方法。这与此相同,只是格式更好

import urllib2
import base64

chimpConfig = {
    "headers" : {
    "Content-Type": "application/json",
    "Authorization": "Basic " + base64.encodestring("hayden:MYSECRETAPIKEY").replace('\n', '')
    },
    "url": 'https://us12.api.mailchimp.com/3.0/'}

#perform authentication
datas = None
request = urllib2.Request(chimpConfig["url"], datas, chimpConfig["headers"])
result = urllib2.urlopen(request)

谢谢-我应该提到我尝试了很多不同的组合
http://api.foursquare.com
api.foursquare.com
http://api.foursquare.com/v1/
,但这似乎并不能解决问题。我只是在本地服务器上尝试了这一方法,该服务器需要基本身份验证,并在add\u passwo中使用URLrd它工作得很好。因此,我建议正在进行其他操作。这仅在http响应包含未经授权的代码401和标题
'WWW-Authenticate'
;请参阅时才起作用。我想知道问题是否在于该站点没有返回
'WWW-Authenticate'
标题。您可以使用
try:urlib2.url检查这一点打开(req)urllib2.HTTPError除外,e:print e.headers
。HTTP错误505:HTTP版本不受支持;(与paypal身份验证配合使用(以便接收访问令牌)同样。非常感谢,伙计!请注意,您可以简单地调用
base64.b64encode
而不是
base64.encodestring
,这样您就不需要替换换行符了。谢谢@TreyStout,我编辑了解决方案以包含您的建议。这里有类似的问题。在已加载授权页面的浏览器内容中,如果我单击“取消”按钮,我可以请参阅密码页内容“是否允许打开URL,如
urllib2.urlopen('http://USER:PASS@example.com/path/)
这是另一个问题。您确定这不适用于标准的
urllib2