[英]How can i capture result of requests.POST in Python?
我對Python很陌生,並試圖模擬在Postman中測試API POST請求時捕獲的結果。 我基本上是試圖捕獲使用Post方法生成的令牌。
我在郵遞員中有以下詳細信息,這給了我令牌詳細信息。
授權選項卡: 類型:基本身份驗證, 用戶名 , 密碼
標題標簽: Content-Type :application / x-www-form-urlencoded, 授權: #This is Auto-Filled
正文標簽 : 原始: grant_type = client_credentials&scope = read_snfa_unifiedgateway
現在,當我在Postman中使用上述詳細信息運行時,它為我提供了訪問令牌的結果,但是當我使用以下Python代碼使用相同的詳細信息時,我僅獲得<Response [200]>
作為響應變量的結果。
import requests
url = 'https://appurl.com/oauth/ls/connect/token'
body = 'grant_type=client_credentials&scope=read_snfa_unifiedgateway'
response = requests.post(
url,
body,
auth=HTTPBasicAuth('my_username', 'my_password'), headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
我該如何更改上述Python代碼,使我可以按照以下在Postman中獲得的格式獲得結果,然后在下一個操作中將access_token值用於另一種POST方法?
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbsdfsdfsdfsdfcsdcsdvarglkwelkrmgmasdmnfjtugnasmdfisdfmmmmpeirhg_-e60s72zgV8Gn2hUiWwlelNQhJongUW6fxwD42c1N5u4R2JJdrj5V_bIwnvY_C_l5wHlIFSQRE1E-5KzP7WG9XjmV9oXRXXGjNhwRqEocGdiEMjcibyiYQNZmG2h-GbsTKvCc21hRNhyRF_y4mdwVUytAXT68TuwZxsTbjUzEvPdwd2JZaFnD9Elo7akSk2ROnRMxN70fsoLzEK71kbzoYkti1jX_V8i_s6K0wmLma-x4nc2kW5mpFM0R9NPqH-kGf-4ZUKim03frifpsGl6Nqo-eN5oZ-b6YgK56mSjxpxQ",
"expires_in": 3600,
"token_type": "Bearer"
}
使用response.json()
以dict格式獲取結果。 您可以使用
access_token = response.json()['access_token']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.