我在 Python 中有下面的代碼,它正在為 OAuth2 令牌發出 POST 請求。它使用基本身份驗證。該代碼工作正常,但我想將其“翻譯”為 curl。代碼:#Authorization: Basic c29tZV91c2VyOnBhc3M=#some_user:pass = base64decode('c29tZV91c2VyOnBhc3M=') def get_access_token():burp0_url = "https://myurl:443/api/oauth/token"burp0_headers = {"Accept": "application/json", "Authorization": "Basic c29tZV91c2VyOnBhc3M=", "Content-Type": "application/x-www-form-urlencoded", "Connection": "close", "Accept-Encoding": "gzip, deflate", "User-Agent": "okhttp/3.0.1"}burp0_data={"grant_type": "client_credentials"}return json.loads(requests.post(burp0_url, headers=burp0_headers,data=burp0_data).text)['access_token']我的猜測是它看起來像這樣:curl -v -XPOST -H 'Authorization: Basic c29tZV91c2VyOnBhc3M=' --header 'Accept: application/json' --header 'Connection: close' --header 'Accept-Encoding: gzip, deflate' --header 'User-Agent: okhttp/3.0.1' --data '{"grant_type": "client_credentials"}' https://myurl:443/api/oauth/token但是,我不斷收到 HTTP/1.1 400 和以下內容* 寫入主體失敗 (0 != 10)* 寫入數據失敗* 停止了暫停流!* 關閉連接 0你能幫幫我嗎?
1 回答

慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
看起來您忘記將"Content-Type": "application/x-www-form-urlencoded"
標題復制到 curl 命令中。
這也表明數據不是像您目前所做的那樣作為 JSON 字符串提交,而是作為常規表單數據提交。您可能可以使用-F 'grant_type=client_credentials'
它并刪除--data
參數。
添加回答
舉報
0/150
提交
取消