Spotify API 授权代码流错误:"grant_type parameter is missing"(Python)



我正在按照文档使用授权代码流连接到Spotify。我可以获取授权码,但不能使用它来获取访问令牌。我一直得到的错误是

{'error': 'unsupported_grant_type', 'error_description': 'grant_type parameter is missing'}

这是我在回调函数中的代码:

from requests import post
client_creds = f'{CLIENT_ID}:{CLIENT_SECRET}'
client_creds_64 = base64.b64encode(client_creds.encode())
token_data = {
'grant-type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI #currently on localhost and whitelisted on Spotify
}
token_header = {
'Authorization': f'Basic {client_creds_64.decode()}',
'Content-Type': 'application/x-www-form-urlencoded'
}
response = post('https://accounts.spotify.com/api/token', data=token_data, headers=token_header)

如果我使用客户端凭据流并替换token_data:,我可以使后请求工作

token_data = {
'grant_type': 'client_credentials'
}

知道为什么授权代码流不起作用吗?不要认为我在token_data中缺少一个参数。。。

grant_type中存在类型错误:

token_data = {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI #currently on localhost and whitelisted on Spotify
}

相关内容

最新更新