[英]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.