如何对 python 的库请求使用 jwt 授权?

How to use jwt authorization with python's library requests?

我正在开发一个 Flask RESTFULL API,其中 flask_jwt_extended 库作为授权扩展。

现在,我有两个用于注册用户和登录的常用资源,它们运行良好。使用登录资源,用户可以提供他们的电子邮件和密码并取回令牌。来自 Postman,此令牌使用不记名令牌模式按预期工作。

但是,我需要直接从 Python3 终端发出一些请求。到目前为止,我在我的请求中使用了一个简单的 name-password 身份验证,因此一个请求示例是:

import requests as rs

rs.get('http://localhost:5000/api/samples', auth = ('user', 'pass'))

现在,使用 jwt 令牌,我正在尝试以下操作:

import requests as rs

# xxxxxx is a really long string got previously with login resource

rs.get('http://localhost:5000/api/samples', auth = ("xxxxxx"))

但是这种方法给我:

TypeError: 'str' object is not callable

在其他帖子中,我检查过这种类型的授权是通过 headers 进行的,所以我也尝试过:

import requests as rs

# xxxxxx is a really long string got previously with login resource

rs.get('http://localhost:5000/api/samples', headers = {'Authorization': 'access_token xxxxxx'})

此处的错误是即时 401 http 响应。

希望有人能帮助我!

提前致谢!

如果有人来这里寻找答案:你只需要添加一个header就像下面的

headers = {'Authorization': 'Bearer {token}'}

来源:https://reqbin.com/req/python/5k564bhv/get-request-bearer-token-authorization-header-example