我想使用基本授權獲取授權令牌。我使用我的用戶名和密碼發送了一個發布請求,但要獲取令牌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
提交
取消