Getting 401 Invalid access token error while running curl command on command prompt

76 views Asked by At

I was trying to use Spotify API by using curl command. First used the web API, clicked search and then entered the artist name. It generated the curl command which I was supposed to run on command prompt.

Whenever I try to run that it is giving me:

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

https://developer.spotify.com/documentation/web-api/reference/search link from where I generated the code. Can anybody please help and me and let me know I am doing wrong.

1

There are 1 answers

0
Bench Vue On

if use Git BASH and jq, API call by curl much more easy.

Assign environment variable

for Spotify Credential (CLIENT_ID, CLIENT_SECRET)

CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  • Note no single quote or double quote

enter image description here

Get Access Token

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')

enter image description here

Call Spotify search query

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

enter image description here