[英]Translate cURL headers command to python-requests
我需要將包含令牌的POST傳遞給網站以獲取auth密鑰,我已成功使用cURL與以下語句完成了它:
curl -D - --request POST https://login.website.com/g/aaa/authorize --data-urlencode token=[TOKEN]
但是,使用Python請求模塊似乎無法獲得相同的結果:
auth_token = requests.post('https://login.website.com/g/aaa/authorize', data=token)
auth_token.text
只是給我'Unauthorized'
我嘗試將數據kawrg傳遞為:
token=
添加到字符串的開頭 params
而不是數據並將其傳遞給JSON(這使我得到了unexpected argument: {"token" : "value....."}
) 在這種情況下, -D
和--data-urlencode
(與--data-urlencode
相對)的意義是什么,我是否應該嘗試將標頭作為參數轉儲到--data
requests.post()
?
ЕDIT:在cURL輸出中,還會在任何json開始之前打印auth_key:
Set-Cookie: auth_key=[auth_key]; Domain=.website.com; expires=Fri, 30-Sep-2016 01:41:09 GMT; httponly; Max-Age=14399; Path=/
找到答案, requests.json()
需要被用來代替json.dumps(data)
,后者是不正確格式的,因此,此代碼制定了罰款:
s.token = requests.post(url, data=data)
s.recieved = s.token.json()
t = requests.post(url, data=s.recieved)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.