使用Curl请求Spotify访问令牌遇URL拒绝及411错误的解决咨询
错误原因拆解
- "URL rejected: Bad hostname":要么是Spotify API URL拼写错误(比如把
https://accounts.spotify.com/api/token写成了错误地址),要么是命令里的引号、空格格式混乱,导致curl把部分请求头/参数内容当成主机名解析。 - 411 Length Required:Spotify的token接口要求POST请求必须携带请求体(需传递
grant_type、client_id、client_secret这些参数),如果没传请求体,或者curl未正确识别请求体,服务器就会返回该错误,提示需要Content-Length头(curl通常会自动添加,但无请求体时无法生成)。 - '-H'未被系统识别:属于命令格式问题,比如在Windows CMD里引号规则和Linux/macOS的bash不同,或者
-H前有多余空格、引号配对错误,导致系统把-H当成独立命令而非curl的参数。
正确的Curl命令示例
Linux/macOS(Bash/Zsh)
curl -X POST "https://accounts.spotify.com/api/token" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=client_credentials&client_id=你的客户端ID&client_secret=你的客户端密钥"
Windows CMD
curl -X POST "https://accounts.spotify.com/api/token" ^ -H "Content-Type: application/x-www-form-urlencoded" ^ -d "grant_type=client_credentials&client_id=你的客户端ID&client_secret=你的客户端密钥"
Windows PowerShell
Invoke-RestMethod -Uri "https://accounts.spotify.com/api/token" -Method Post -ContentType "application/x-www-form-urlencoded" -Body @{ grant_type = "client_credentials" client_id = "你的客户端ID" client_secret = "你的客户端密钥" }
注意:将命令里的你的客户端ID和你的客户端密钥替换为Spotify开发者后台获取的真实信息。
内容的提问来源于stack exchange,提问作者TheOx
相关产品推荐
相关产品推荐

