我正在使用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)
我收到的响应总是
<Response [400]>
。
这里是文档,第四步 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 code={auth_code} -d redirect_uri={self.redirect_uri} -d code_verifier={code_verifier} https://accounts.spotify.com/api/token