1

I would like to retrieve information from the Spotify API with the command line, for example like this:

curl "https://api.spotify.com/v1/search?type=artist&q=<someartist>"

But when I do that I get:

{
  "error": {
    "status": 401,
    "message": "No token provided"
  }
}

I have already created an app in my Spotify developer account. Could someone please walk me through the process how to pass my credentials along with my search request? I don't want to program an application or anything. I just want to retrieve information from command line.

Hope someone can help!

nacht-falter
  • 143
  • 1
  • 8
  • https://developer.spotify.com/documentation/general/guides/authorization-guide/ – 0stone0 Apr 16 '21 at 16:00
  • 1
    Thank you, I had already seen that page, but I can't get it to work. Could you help me figure out the correct command? – nacht-falter Apr 16 '21 at 17:27
  • It's not just 1 command, it's a flow of receiving tokens and using those to authenticate your app. 'How this works' is too broad as question here on SO. I recommend trying some flows, google, youtube and SO are really help full, you're not the first one trying to use the Spotify Api ;) If you're stuck after some attempts, [edit] your question, show us what you've tried and the errors they produce. Consider reading: [ask]. – 0stone0 Apr 16 '21 at 19:02
  • 1
    I did it! Thank you for believing in me! :) – nacht-falter Apr 17 '21 at 00:17

2 Answers2

8

So, I spent some more time deciphering the instructions at https://developer.spotify.com/documentation/general/guides/authorization-guide/ and I actually found a rather simple solution for this.

What I want to do is retrieve Spotify album URIs from the Spotify Web API by searching for specifc albums. Since I don't need refreshable access tokens for that and I don't need to access user data, I decided to go with the Client Credentials Authorization Flow (https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow). Here's what I did:

  1. Create an application on the Dashboard at https://developer.spotify.com/dashboard/applications and copy client ID and client secret

  2. Encode client ID and Client secret with base64:

    echo -n <client_id:client_secret> | openssl base64
    
  3. Request authorization with the encoded credentials, which provided me with an access token:

    curl -X "POST" -H "Authorization: Basic <my_encoded_credentials>" -d grant_type=client_credentials https://accounts.spotify.com/api/token
    
  4. With that access token it's possible to make requests to the API endpoints, that don't need user authorization, for example:

     curl -H "Authorization: Bearer <my_access_token>" "https://api.spotify.com/v1/search?q=<some_artist>&type=artist"
    

All available endpoints can be found here: https://developer.spotify.com/documentation/web-api/reference/

In my case I want to read input in the Terminal in the format "artist album" and output the corresponding spotify URI, which is exactly what the following shell script does:

#!/bin/bash
artist=$1
album=$2
creds="<my_encoded_credentials>"
access_token=$(curl -s -X "POST" -H "Authorization: Basic $creds" -d grant_type=client_credentials https://accounts.spotify.com/api/token | awk -F"\"" '{print $4}')
result=$(curl -s -H "Authorization: Bearer $access_token" "https://api.spotify.com/v1/search?q=artist:$artist+album:$album&type=album&limit=1" | grep "spotify:album" | awk -F"\"" '{print $4 }')

I can then run the script like this:

myscript.sh some_artist some_album

and it will output the album URI.

nacht-falter
  • 143
  • 1
  • 8
-1

It may be too late to ask this but how did you pass the artist and the album that contain spaces. I found ' and " didn't work so I had to do Deep%20Purple Made%20In%20Japan as an example.

  • 1
    Use "Add a comments" for comments, and use "Your Answer" only for answers. – Ximzend Nov 29 '22 at 06:19
  • The question does contain a relevant answer in a way, since his idea to encode whitespaces is essentially correct, but yeah: it would have made more sense as a comment. Anyway: You can encode whitespaces with parameter expansion by doing something like `artist=${1// /%20}` and `album=${2// /%20}`. Then you can run the script with quotes like `myscript.sh "some artist" "some album"`. – nacht-falter Nov 30 '22 at 09:06
  • I also found I had to escape special characters like ', [, ], ( and ). I may find others as well. – Robert Guest Nov 30 '22 at 20:17
  • Welcome @RobertGuest! As @Ximzend said, the query portion of your answer is better suited as a comment on the original question. In the future, I suggest splitting the two. – azarc3 Dec 01 '22 at 15:16