gpt4 book ai didi

Flask werkzeug request.authorization 不是 Authorization 头文件

转载 作者:行者123 更新时间:2023-12-03 15:25:11 28 4
gpt4 key购买 nike

我正在发布一些 JSON 数据并添加一个 Authorization标题。但是,请求对象没有正确的授权属性。 HTTP_AUTHORIZATIONheaders两者都显示正确的授权详细信息。

{'authorization': None,
'cookies': {},
'environ': {'CONTENT_LENGTH': '81',
'CONTENT_TYPE': u'application/json',
'HTTP_AUTHORIZATION': 'testkey:',
'HTTP_CONTENT_LENGTH': '81',
'HTTP_CONTENT_TYPE': 'application/json',
'HTTP_HOST': 'test',
'PATH_INFO': '/v1/test',
'QUERY_STRING': '',
'REQUEST_METHOD': 'POST',
'SCRIPT_NAME': '',
'SERVER_NAME': 'test',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.1',
'flask._preserve_context': False,
'werkzeug.request': <Request 'http://test/v1/test' [POST]>,
'wsgi.errors': <open file '<stderr>', mode 'w' at 0x10d5471e0>,
'wsgi.input': <_io.BytesIO object at 0x11074c410>,
'wsgi.multiprocess': False,
'wsgi.multithread': False,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)},
'headers': EnvironHeaders([('Authorization', testkey:'), ('Host', u'test'), ('Content-Length', u'81'), ('Content-Type', u'application/json')]),
'shallow': False,
'url': u'http://test/v1/test',
'url_rule': <Rule '/v1/test' (POST, OPTIONS) -> testresource>,
'view_args': {}}

最佳答案

request.authorization仅当您具有有效的 Basic Authorization 时才设置属性或 Digest Authorization标题; Authorization header 具有特定格式,其中 header 值中的第一个单词设置类型,并且该属性仅处理这两种特定类型(由 BasicDigest 类型关键字标记)。

来自 AuthorizationMixin.authorization attribute documentation

The Authorization object in parsed form.



接下来是 Authorization object docs :

Represents an Authorization header sent by the client. You should not create this kind of object yourself but use it when it’s returned by the parse_authorization_header function.



这是 documented as :

Parse an HTTP basic/digest authorization header transmitted by the web browser. The return value is either None if the header was invalid or not given, otherwise an Authorization object.



大胆强调我的。

你的标题不是一个有效的标题;它没有 BasicDigest类型指示器。如果您确实有这样的 header ,该函数将返回 None 以外的其他内容。 :
>>> from werkzeug.http import parse_authorization_header
>>> parse_authorization_header('testkey:')
>>> parse_authorization_header('testkey:') is None
True
>>> parse_authorization_header('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==')
{'username': 'Aladdin', 'password': 'open sesame'}
>>> result = _
>>> type(result)
<class 'werkzeug.datastructures.Authorization'>
>>> result.username
'Aladdin'
>>> result.password
'open sesame'

我用了 Basic在那里键入 header,其中 header 值的其余部分是 base-64 编码的用户名和密码对,由 : 分隔。冒号。

如果要实现自己的身份验证方案,只需访问 header 本身并手动解析即可。
auth = request.headers.get('authorization')

关于Flask werkzeug request.authorization 不是 Authorization 头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27630122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com