Spotipy – 401 token error "no token proviced" with .search function

Question:

I have a list of dictionaries that contain album information which I’m trying to use to search within Spotify and then add to users’ saved albums. I tried following the examples given in the spotipy documentation, however, I’m getting an issue when using the search function via spotipy.

SCOPE = 'user-library-modify'

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, scope=SCOPE))

for album in newMusic:
    albumName = album["Title"]
    artistName = f"{album['Last Name']}+{album[' First Name']}"
    getRecord = sp.search(q = f"artist:{artistName}&album:{albumName}", type='album')  
    print(getRecord)

I provided the redirect url when prompted after running, but this results in a 401 response for each album result as such:

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

I have the .cache file with the access and refresh tokens but it still says no token is provided. I thought maybe I was entering the query incorrectly, but I don’t see anything wrong. This is how an example end url looks:

https://api.spotify.com/v1/search?query=artist:band+name&album:album+name&type=album&offset=0&limit=10

What am I doing wrong here? How can I get my access code recognized?

Asked By: Kyle M

||

Answers:

Found the issue; the query entry format was wrong. I found this answer to a past question that explains my problem: Using python to get a track from the spotify API by using search Endpoint

The artist, album, etc. do not need to be declared separately (e.g. "artist: _", "album: _"). They should just be combined together like:

https://api.spotify.com/v1/search?query=first+name+last+name+album+title&type=album&offset=0&limit=10

Weirdly, this contradicts what is in the Spotify API documentation where an example query value is given as such:

Example value:
"remaster%20track:Doxy+artist:Miles%20Davis"

https://developer.spotify.com/documentation/web-api/reference/#/operations/search

Answered By: Kyle M
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.