我收到:'HTTP/1.1 400 Bad Request\r\n' 但我不明白为什么。看起来它进行了身份验证,然后进行了重定向,然后现在不起作用。为什么会这样?
我原以为这是标题,它缺少内容类型,但即使添加它也会产生相同的结果..
headers = {
'basic_auth': 'brofewfefwefewef:EKAXsWkdt5H6yJEmtexN',
'Content-Type': 'application/json'
}
client = Client(ClientConfig(), headers=headers, refresh=True)
class FileDownloader(object):
...Line 152...
def _get_http_pool(self, secure=True):
if secure:
_http = urllib3.PoolManager(cert_reqs=str('CERT_REQUIRED'),
ca_certs=certifi.where())
else:
_http = urllib3.PoolManager()
if self.headers:
content_type = self.headers.get('Content-Type')
if 'Content-Type' in self.headers:
del self.headers['Content-Type']
_headers = urllib3.util.make_headers(**self.headers)
_http.headers.update(_headers)
if content_type:
_http.headers['content-type'] = content_type
print(_http.headers)
return _http
https://github.com/JMSwag/PyUpdater/blob/master/pyupdater/client/downloader.py 第 366 行是下载本身开始的地方。这至少可以说是令人困惑的。
错误:
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.bitbucket.org
send: b'GET /2.0/repositories/Anexampleuser/repo/downloads/keys.gz HTTP/1.1\r\nHost: api.bitbucket.org\r\nAccept-Encoding: identity\r\nauthorization: Basic YnJvZmV3ZmVmd2VmZXdlZjpFS0FYc1drZHQ1SDZ5SkVtdGV4Tg==\r\n\r\n'
reply: 'HTTP/1.1 302 Found\r\n'
DEBUG:urllib3.connectionpool:https://api.bitbucket.org:443 "GET/2.0/repositories/Anexampleuser/repo/downloads/keys.gz HTTP/1.1" 302 0
DEBUG:urllib3.util.retry:Incremented Retry for (url='https://api.bitbucket.org/2.0/repositories/Anexampleuser/repo/downloads/keys.gz'): Retry(total=2, connect=None, read=None, redirect=None, status=None)
INFO:urllib3.poolmanager:Redirecting https://api.bitbucket.org/2.0/repositories/Anexampleuser/repo/downloads/keys.gz -> https://bbuseruploads.s3.amazonaws.com/a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/3fc0be6d-ca69-42d3-9711-fbb5cfd2bc38/keys.gz?Signature=ZQxeUTvYC3Q%2Fo1aaS1CSuzyit0Q%3D&Expires=1515976464&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=n.ymY11KRkq36Xozy25aChvfUT.YzTf5&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bbuseruploads.s3.amazonaws.com
header: Server header: Vary header: Content-Type header: X-OAuth-Scopes header: Strict-Transport-Security header: Date header: Location header: X-Served-By header: ETag header: X-Static-Version header: X-Content-Type-Options header: X-Accepted-OAuth-Scopes header: X-Credential-Type header: X-Render-Time header: Connection header: X-Request-Count header: X-Frame-Options header: X-Version header: Content-Length send: b'GET /a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/3fc0be6d-ca69-42d3-9711-fbb5cfd2bc38/keys.gz?Signature=ZQxeUTvYC3Q%2Fo1aaS1CSuzyit0Q%3D&Expires=1515976464&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=n.ymY11KRkq36Xozy25aChvfUT.YzTf5&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22 HTTP/1.1\r\nHost: bbuseruploads.s3.amazonaws.com\r\nAccept-Encoding: identity\r\nauthorization: Basic YnJvZmV3ZmVmd2VmZXdlZjpFS0FYc1drZHQ1SDZ5SkVtdGV4Tg==\r\n\r\n'
reply: 'HTTP/1.1 400 Bad Request\r\n'
请您参考如下方法:
BitBucket API 为 /downloads/ 端点返回 302,所以 Authorization header 被执行到下一个请求,而 Amazon 不喜欢那个 header,所以它返回 400。解决方法是手动重新创建重定向请求。例如:(省略错误检查)
import urllib3
http_pool = urllib3.PoolManager()
req = http_pool.urlopen(
'GET', 'https://api.bitbucket.org/2.0/repositories/brofewfefwefewef/eee/downloads/keys.gz',
redirect=False, headers={'Authorization': 'Basic YnJvZmV3ZmVmd2VmZXdlZjpFS0FYc1drZHQ1SDZ5SkVtdGV4Tg=='})
redirected_req = http_pool.urlopen('GET', req.headers['Location'], preload_content=False)
with open('keys.gz', 'wb') as f:
f.write(redirected_req.read())
顺便说一下,您的访问 token 仍然可用。
