【问题标题】:Python urllib2 Basic Auth ProblemPython urllib2 基本认证问题
【发布时间】:2011-01-25 07:32:30
【问题描述】:

更新:根据 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: */*

由于某种原因,我的代码似乎没有发送身份验证 - 有人看到我遗漏了什么吗?

谢谢

-西蒙

【问题讨论】:

  • 我想知道问题是否在于该站点没有返回 'WWW-Authenticate' 标头。您可以使用try: urllib2.urlopen(req) except urllib2.HTTPError, e: print e.headers See this SO post answer 进行检查。

标签: python authentication urllib2


【解决方案1】:

问题可能是 Python 库,根据 HTTP 标准,首先发送未经身份验证的请求,然后只有当它通过 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

【讨论】:

  • HTTP 错误 505:不支持 HTTP 版本;(
  • 也适用于贝宝身份验证(为了接收 access_token)。非常感谢,伙计!
  • 请注意,您可以简单地调用base64.b64encode 而不是base64.encodestring,然后您不需要替换换行符。
  • 感谢@TreyStout,我编辑了解决方案以包含您的建议。
  • 这里有类似问题..在已加载授权页面的浏览器内容中,如果我单击取消按钮,我可以看到密码页面内容
【解决方案2】:

这是我用来处理我在尝试访问 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】:

    我建议当前的解决方案是使用我的包urllib2_prior_auth,它很好地解决了这个问题(我在inclusion 上工作到标准库。

    【讨论】:

    • 是否允许打开像urllib2.urlopen('http://USER:PASS@example.com/path/')这样的网址
    • 这是另一个问题。您确定这不适用于标准 urllib2
    【解决方案4】:

    (复制粘贴/改编自https://stackoverflow.com/a/24048772/1733117)。

    首先,您可以继承urllib2.BaseHandlerurllib2.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
    

    那如果你和我一样懒,那就全局安装handler

    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)
    

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

      • 谢谢 - 我应该提到我尝试了多种不同的组合http://api.foursquare.comapi.foursquare.comhttp://api.foursquare.com/v1/,但这似乎并不能解决问题。
      • 我只是在这里对需要基本身份验证的本地服务器进行了尝试,并且使用 add_password 中的 URL 可以正常工作。因此,我建议其他事情正在进行中。
      • 这只有在http响应包含代码401 Unauthorized 标头'WWW-Authenticate'时才有效;见this SO post answer
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 2012-01-09
      • 2011-10-17
      • 1970-01-01
      相关资源
      最近更新 更多