使用Spotify API客户端凭证流认证时遇unsupported_grant_type错误求助
unsupported_grant_type错误 老哥,我太懂你遇到这个错误的郁闷了——Spotify的客户端凭证流看着简单,但细节没踩对就会出这个糟心的提示。我帮你梳理几个最可能的问题点,按顺序排查应该就能搞定:
先盯紧请求头的
Content-Type
Spotify对这个请求的格式要求特别严格,必须设置成application/x-www-form-urlencoded。很多人习惯用JSON发请求,但这里绝对不行,哪怕你参数写对了,Content-Type不对直接触发unsupported_grant_type。比如用Postman的话,要选“x-www-form-urlencoded”选项,别选Raw JSON。确保
grant_type参数完全精准
这个参数必须一字不差是client_credentials——下划线不能错、大小写全小写,别手滑写成client_credential(少了个s)或者Client_Credentials(大写了)。Spotify对这个参数的校验非常严格,差一点都不行。参数要放在请求体里,别塞URL里
别把grant_type、client_id、client_secret这些参数拼在请求URL的查询字符串里,必须通过POST请求的请求体以表单编码的形式传递。比如正确的请求结构示例:import requests token_url = "https://accounts.spotify.com/api/token" auth_data = { "grant_type": "client_credentials", "client_id": "你的客户端ID", "client_secret": "你的客户端密钥" } headers = {"Content-Type": "application/x-www-form-urlencoded"} response = requests.post(token_url, data=auth_data, headers=headers) print(response.json())顺便核对下客户端凭证
虽然错误提示指向grant_type,但有时候如果client_id或client_secret复制错了(比如多了空格、少了字符),也可能返回误导性的错误。去Spotify开发者后台再确认下你的应用凭证是不是完全正确。
按这几步排查下来,基本就能解决这个问题了,我之前帮好几个开发者踩过这个坑,大多是Content-Type或者grant_type拼写的问题。
内容的提问来源于stack exchange,提问作者Valkyrie




