更新:基于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)
不幸的是,它仍然不会生成Authorization
标题(每个Wireshark):(
我在通过urllib2发送基本AUTH时遇到问题。我看了this article,然后按照例子。我的代码:
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在Wire上看到以下内容:
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
答案 0 :(得分:194)
问题可能是,每个HTTP-Standard的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)
遇到与您相同的问题并从此主题找到解决方案:http://forums.shopify.com/categories/9/posts/27662
答案 1 :(得分:5)
(复制粘贴/改编自https://stackoverflow.com/a/24048772/1733117)。
首先,您可以继承urllib2.BaseHandler
或urllib2.HTTPBasicAuthHandler
,并实施http_request
,以便每个请求都有相应的Authorization
标头。
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)
答案 2 :(得分:5)
这是我用来处理尝试访问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)
答案 3 :(得分:4)
第二个参数必须是URI,而不是域名。即。
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "http://api.foursquare.com/", username, password)
答案 4 :(得分:0)
我建议当前的解决方案是使用我的包urllib2_prior_auth,它很好地解决了这个问题(我在inclusion处理标准库。