在命令提示符下运行curl命令时出现401无效访问令牌错误

问题描述 投票:0回答:1

我试图通过使用curl命令来使用Spotify API。首先使用Web API,点击搜索,然后输入艺术家姓名。它生成了我应该在命令提示符下运行的curl 命令。

每当我尝试跑步时,它都会给我:

{
  "error": {
    "status": 401,
    "message": "Invalid access token"
  }
}

https://developer.spotify.com/documentation/web-api/reference/search 我生成代码的链接。 有人可以帮助我并让我知道我做错了吗?

spotify spotify-app
1个回答
0
投票

如果使用Git BASHjq,curl调用API会更容易。

分配环境变量

Spotify 凭证(CLIENT_ID、CLIENT_SECRET)

CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  • 注意不要单引号或双引号

获取访问令牌

ACCESS_TOKEN=$(curl -s -X POST 'https://accounts.spotify.com/api/token' -H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET" \
 | jq -r '.access_token')

调用 Spotify 搜索查询

curl --request GET \
  --url 'https://api.spotify.com/v1/search?q=remaster%2520track%3ADoxy%2520artist%3AMiles%2520Davis&type=album' \
  --header 'Authorization: Bearer '$ACCESS_TOKEN

© www.soinside.com 2019 - 2024. All rights reserved.