我正在使用 Spotify API 开发一个应用程序。我的问题是我正在尝试获取access_token但它不起作用。在文档中,它说我需要发送编码为 application/x-www-form-urlencoded 的正文,所以我搜索了一下,它应该只需设置request_body为字典即可工作。
这是我的函数的代码:
def get_access_token(self):
auth_code, code_verifier = self.get_auth_code()
endpoint = "https://accounts.spotify.com/api/token"
# as docs said data should be encoded as application/x-www-form-urlencoded
# as internet says i just need to send it as a dictionary. However it's not working
request_body = {
"client_id": f"{self.client_ID}",
"grant_type": "authorization_code",
"code": f"{auth_code}",
"redirect_uri": f"{self.redirect_uri}",
"code_verifier": f"{code_verifier}"
}
response = requests.post(endpoint, data=request_body)
print(response)
Run Code Online (Sandbox Code Playgroud)
我得到的回应始终是<Response [400]>
这里是文档,步骤 4 https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow-with-proof-key-for- code-exchange-pkce
注意:我尝试将其作为curl执行,它工作正常,我不确定我在python代码中做错了什么,命令如下:
curl -d client_id={self.client_ID} -d grant_type=authorization_code -d …Run Code Online (Sandbox Code Playgroud)