Spotipy 401 / 405 No token provided

Question:

I have been working on a Spotify / Spotipy application which will add the current song to a certain playlist.
My functions for getting the current song and showing a playlist are all doing fine. My function for adding a song to a playlist is not.

I am getting the following error in my IDE:

http status: 405, code:-1 – https://api.spotify.com/v1/users/myUserID/playlists/myPlayListID/tracks:
error

When I copy paste the link to the browser, I am getting the following error:

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

As far as I’m aware I’m providing a token.

Here is my function which makes the Spotify object

def __createSpotifyObject(self):
    """Will create a Spotify object and return it"""

    # Defining the scope(s) of the application
    scope = "playlist-modify-public playlist-modify-private user-read-currently-playing"

    # Getting the token
    token = util.prompt_for_user_token(username=USER_NAME, scope=scope, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri="https://localhost/")

    # Returning our Spotify object
    return spotipy.Spotify(auth=token)

Here is the function which tries to add a song to my playlist (this is where the error occurs)

I’ve tried catching the exception (which it did) and then trying to make a new Spotify object so it will refresh the token or something like that. It just gives me the same error.

def addCurrentSongToSelectedPlaylist(self):
    """Will add the currently playing song to the selected playlist"""

    # Checking if a playlist has been selected
    if (len(self.__selectedPlaylist ) < 1):

        print("No playlist selected")
        return

    # Getting the current song
    currentSong = self.getCurrentSong()

    # Adding the current song id to the selected playlist
    try:
        self.__spotify.user_playlist_add_tracks(USER, self.__selectedPlaylist, [currentSong["id"]])

    except spotipy.client.SpotifyException:
        # Re authenticating
        self.__spotify = self.__createSpotifyObject()
        self.__spotify.user_playlist_add_tracks(USER, self.__selectedPlaylist, [currentSong["id"]])

At this point the only thing I can think of is that the show playlist / show current playing song actions require fewer permissions, and that is why they work and the add song to playlist won’t.

Asked By: Erikv99

||

Answers:

I found out I implemented the user wrong. Instead of username?si=someNumbers, I needed to just enter the username.

  • Copy and pasting the address of the error message in to the browser is useless since it is an API call, and it will always give back 401: no token provided due to trying to access it without a token.
  • I just stumbled across this beautiful list of what every error code means: Web API. This doesn’t just apply to the web API, but also to the Spotipy library. Check the error code in your IDE or editor against the list on this page.
Answered By: Erikv99
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.