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.
I found out I implemented the user wrong. Instead of username?si=someNumbers, I needed to just enter the username.