[英]Why does this curl command work but not my post request via python?
我正在嘗試通過 python 和請求庫向https: //accounts.spotify.com/api/token 發出 POST 請求,但無法使其正常工作。 我可以通過 curl 命令執行請求:
注意 - 包含在 * * 中的參數是正確的,並且在 curl 請求中有效
curl -H "Authorization: Basic *base 64 encoded client ID and secret*"
-d grant_type=authorization_code -d code=*auth code* -d
redirect_uri=https%3A%2F%2Fopen.spotify.com%2F
https://accounts.spotify.com/api/token
並且請求工作得很好,但是當我嘗試在 python 中提出我認為完全相同的請求時,我總是得到相同的錯誤請求錯誤
headers = {
"Authorization": "Basic *base64 encoded client ID and secret*"
}
params = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": "https://open.spotify.com/"
}
response = requests.post(
url,
params=params,
headers=headers
)
如果你能幫我弄清楚這兩個請求有何不同,以及為什么 python 似乎永遠無法工作,那將是驚人的。
有關參數,請參見https://developer.spotify.com/documentation/general/guides/authorization-guide/的第 2 節
您在代表data
的curl
請求中使用-d
標志。
因此,您還應該在 Python POST
請求中將參數作為data
傳遞:
headers = {
"Authorization": "Basic *base64 encoded client ID and secret*"
}
params = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": "https://open.spotify.com/"
}
response = requests.post(
url,
data=params,
headers=headers
)
似乎您將有效負載置於錯誤的參數下,嘗試將params
更改為json
或data
(取決於 API 接受的請求類型):
response = requests.post(
url,
json=params,
headers=headers
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.