【问题标题】:bad request error 400错误的请求错误 400
【发布时间】:2018-06-22 18:06:45
【问题描述】:

我想用客户端配置复制以下内容。

Curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" http://localhost:7990/rest/api/1.0/projects
https://developer.atlassian.com/server/bitbucket/how-tos/example-basic-authentication/

这是在私人仓库中下载用于更新 exe 的文件。

我在使用 bitbucket 时经常遇到 400 个错误。

我有什么:

client = Client(ClientConfig(), headers={'basic_auth':'U:P' }, refresh=True)

我想包含这个标题

header = {'Content-Type': 'application/json'}

比如:

client = Client(ClientConfig(), headers={'basic_auth': 'brofewfefwefewef:EKAXsWkdt5H6yJEmtexN'}, header = {'Content-Type': 'application/json'}, refresh=True)

应该修复吗?

至少根据....

“某些 http 客户端软件在发送授权标头之前希望收到身份验证质询,这可能意味着它可能无法按预期运行。在这种情况下,您可能需要将其配置为提供上述授权标头而不是依赖其默认机制。”

还有……

https://stackoverflow.com/questions/8840303/urllib2-http-error-400-bad-request

但对我来说,即使更改 400 https://pastebin.com/V9ibxTRX (此处为完整代码或以下简短版本),我总是会出错

可选的错误消息(它不漂亮,但我确实减少了它):

~~~~~ACCESSING PAGE AND FOUND!~~~
DEBUG:pyupdater.client.downloader:Url for request: https://api.bitbucket.org/2.0/repositories/Username/repository/downloads/keys.gz
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.bitbucket.org
send: b'GET /2.0/repositories/Username/repository/downloads/keys.gz HTTP/1.1\r\nHost: api.bitbucket.org\r\nAccept-Encoding: identity\r\nauthorization: Basic ywafdwafawffwawffawafwfwaawfawfg==\r\n\r\n'
reply: 'HTTP/1.1 302 Found\r\n'

~~~~~~RETRYING REDIRECTION PRESENT (IS THIS THE CAUSE OF ISSUES)??~~
DEBUG:urllib3.connectionpool:https://api.bitbucket.org:443 "GET /2.0/repositories/Username/repository/downloads/keys.gz HTTP/1.1" 302 0
DEBUG:urllib3.util.retry:Incremented Retry for (url='https://api.bitbucket.org/2.0/repositories/Username/repository/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/Username/repository/downloads/keys.gz -> https://bbuseruploads.s3.amazonaws.com/a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/1c87431a-98de-4d97-8c80-000243f81cba/keys.gz?Signature=FvA9X7K9ryM2Ft2mTV7PZefidJY%3D&Expires=1515817377&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=6J830UBC1RFvWz.R6pMDwIiJQNKJjSkm&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bbuseruploads.s3.amazonaws.com

~~HEADERS MIGHT BE ISSUE ACCORDING TO DOCS  https://developer.atlassian.com/server/bitbucket/how-tos/example-basic-authentication/~~

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/1c87431a-98de-4d97-8c80-000243f81cba/keys.gz?Signature=FvA9X7K9ryM2Ft2mTV7PZefidJY%3D&Expires=1515817377&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=6J830UBC1RFvWz.R6pMDwIiJQNKJjSkm&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'
DEBUG:urllib3.connectionpool:https://bbuseruploads.s3.amazonaws.com:443 "GET /a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/1c87431a-98de-4d97-8c80-000243f81cba/keys.gz?
Signature=FvA9X7K9ryM2Ft2mTV7PZefidJY%3D&Expires=1515817377&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=6J830UBC1RFvWz.R6pMDwIiJQNKJjSkm&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22 HTTP/1.1" 400 None

~~UNABLE TO ACCESS PAGE (WAIT, BEFORE IT HAD 'HTTP/1.1 302 Found\r\n' SEE TOP)~~
reply: 'HTTP/1.1 400 Bad Request\r\n'

DEBUG:pyupdater.client.downloader:Resource URL: https://api.bitbucket.org/2.0/repositories/Username/repository/downloads/keys.gz
DEBUG:pyupdater.client.downloader:Got content length of: None
DEBUG:pyupdater.client.downloader:Content-Length not in headers
DEBUG:pyupdater.client.downloader:Callbacks will not show time left or percent downloaded.
DEBUG:pyupdater.client.downloader:Using file as storage since the file is too large

【问题讨论】:

  • 我非常怀疑您使用的是 urllib3 - urllib3 源代码中既没有 Client 也没有 ClientConfig 类。可以发a complete example吗?
  • @phihag 错误消息已满:pastebin.com/V9ibxTRX ctr find F -> 400 。如果你真的想重现,你可以,但需要 5 分钟以上。这里的工作示例。 stackoverflow.com/questions/48047296/… 第 366-368 行显示下载器使用 url lib github.com/JMSwag/PyUpdater/blob/master/pyupdater/client/… 引导您完成它的文档。 pyupdater.org/usage-cli 完成后,您会收到类似的错误。
  • @phihag 与其那样做,不如看看我从他们的文档中得到的例子。我认为用配置复制它应该可以解决。可悲的是,我一无所知,一整天都被困住了,但这很可能是如何解决的
  • 抱歉,您提到的所有帖子都没有接近完成。此外,格式不适合:在这个问题中,您使用auth 初始化客户端,并将列表初始化为headers。在code you link to 中,Client 没有auth 参数,headers 接受一个字典,而不是一个列表。如果您希望任何人能够回答您的问题,则必须包含完整的代码;即可以粘贴到文件中并立即运行的代码。删除所有不必要的杂物。
  • @phihag 我已根据您向我解释的内容更新了问题。如果还是不对,就直说吧,我会澄清的

标签: python curl bitbucket urllib http-error


【解决方案1】:

我不熟悉您用于 client 对象的库,但您应该能够在 headers 字典中设置 Content-Type 标头。

headers = {
    'basic_auth': 'brofewfefwefewef:EKAXsWkdt5H6yJEmtexN', 
    'Content-Type': 'application/json'
}
client = Client(ClientConfig(), headers=headers, refresh=True)

不幸的是,这似乎是不可能的,因为 headers 被传递(解包)到 urllib3.util.make_headers 并且它不接受 content_type 参数。

您也不能在Client 中访问FileDownloader._http.headers,因为它是一个局部变量。
一个可能的FileDownloader'hack':

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

这应该允许您在 Client 中传递 Content-Type 标头。
我不建议修改你的库的源代码,但如果你别无选择......

【讨论】:

  • Bitbucket,只是一个残忍的情妇。我得到:文件“C:\Users\Django\AppData\Local\Continuum\miniconda3\lib\site-packages\pyupdater\client\downloader.py”,第 160 行,在 _get_http_pool # _headers = urllib3.util.make_headers(* *self.headers) #TypeError: make_headers() 得到了一个意外的关键字参数“Content-Type”。 .所以很明显我需要找到一种方法来修改github.com/JMSwag/PyUpdater/blob/master/pyupdater/client/… 以便它允许这样做。呵呵..
  • 让我看看图书馆,如果我发现有用的东西我会告诉你的。
  • 我认为 headers 中不可能有 Content-Type 标头。它使用urllib3.util.make_headers 创建标头,并且只接受这些参数:keep_alive, accept_encoding, user_agent, basic_auth, proxy_basic_auth, disable_cache。也许你可以修补 make_headers 或使用其他一些 hack,尽管我不推荐它。必须有正确的方法来设置 Content-Type 标头。
  • 也许在 downloader.py 中以某种方式添加标题会解决它。作为基本身份验证应该可以工作,但我认为需要这些内容类型才能工作,否则它将把你踢出去。我还是假设
  • 我在办公室工作需要这个,但似乎没人知道。使用 Api 很痛苦
猜你喜欢
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多