【发布时间】:2020-10-17 21:15:03
【问题描述】:
我可以使用 curl 命令访问 JWT 保护的 Restful API,如下所示
#Get the access Token in a variable ID
export ID=`curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "password": "admin", "rememberMe": true, "username": "admin" }' 'http://localhost:8080/api/authenticate' | jq -r .id_token`
#Use this token to access endpoint
curl 'http://localhost:8080/api/downloads' --header 'Content-Type: application/json' --header 'Accept: application/json' --header "Authorization: Bearer $ID"
我的验证部分和获取不记名令牌的python脚本如下:
import requests
LOGIN_URL = "http://localhost:8080/api/authenticate"
ENDPOINT_URL = 'http://localhost:8080/api/downloads'
PARAMS = {'password': 'admin','rememberMe': True, 'username': 'admin' }
r1 = requests.post(LOGIN_URL, data =PARAMS, headers={"Content-Type": "application/json","Accept": "application/json"})
print(r1)
当我尝试通过 python 脚本执行相同操作时,身份验证请求失败并显示消息
需要帮助!
【问题讨论】:
-
请将您的错误描述扩展到“它不工作。”之外!
-
试试
r1.get("id_token")而不是r1.get_dict()['id_token']。 -
通常如果我不确定响应结构并且我知道它是某种字典。我会尝试将
.__dict__粘贴到变量上。r1.__dict__。附言。不是所有东西都支持,但大多数主流包类都有这个实现。
标签: python rest curl python-requests jwt-auth