我想使用基本授权获取授权令牌。我使用我的用户名和密码发送了一个发布请求,但要获取令牌grant_type=client_credentials&scope=Dashboard,请求中必须包含原始文本的正文数据。但我无法grant_type=client_credentials&scope=Dashboard使用 python 在发布请求中发送正文数据。 @task(1)
def login(self):
self.client.post("/OAuth/Token/", {'Username':'abc', 'Password':'12345'})
2 回答
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
self.client.post()返回一个响应对象。您可以在https://requests.readthedocs.io/en/latest/api/#requests.Response查看 api
要在响应中读出某些内容,您可以尝试类似
@task(1)
def login(self):
res = self.client.post("/OAuth/Token/", {'Username':'abc', 'Password':'12345'})
token = res.json()['token']
这会尝试将响应主体处理为 json 并提取令牌字段。如果这不起作用,请提供有关您在回复中看到的内容的详细信息。
慕的地8271018
TA贡献1796条经验 获得超4个赞
请试试这个:
在 URL 中,附加授权类型和范围,如下所示:
/OAuth/Token?grant_type=client_credentials&scope=Dashboard
它看起来像这样
self.client.post("/OAuth/Token?grant_type=client_credentials&scope=Dashboard", {'Username':'abc', 'Password':'12345'})
添加回答
举报
0/150
提交
取消
