[GH-ISSUE #505] HTTPError: 405 Client Error: Method Not Allowed #301

Closed
opened 2026-02-27 23:21:53 +03:00 by kerem · 4 comments
Owner

Originally created by @dursab on GitHub (May 30, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/505

Hello, I am trying to delete songs from a playlist with Spotipy and I end up getting the same error as several peoples described in other issues : http status: 405, code:-1
I tried many adjustments in my script following what had been said in these other issues but no luck so far.

Here is the code that I'm using:

import os
import spotipy
import spotipy.util as util


# Credentials
CLIENT_ID = 'XXXXX'
CLIENT_SECRET = 'XXXXX'
REDIRECT_URI = 'http://localhost:8888/callback'

# Scope
scope = 'user-library-read playlist-modify-public playlist-modify-private'

# User Id :
username = 'XXXXX'

# Erase cache and prompt for user permission
try :
    token = util.prompt_for_user_token(username, scope, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI, cache_path=".cache-{username}")
    print ("SUCCESS!")
except:
    os.remove(f".cache-{username}")
    token = util.prompt_for_user_token(username, scope, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI, cache_path=".cache-{username}")

# Create the spotifyObject
sp = spotipy.Spotify(auth=token)

# Access to Playlists
playlists = sp.current_user_playlists()

def findThePlaylist(playlists):
    playlistID = ''
    for i, item in enumerate(playlists['items']):
        if item['name'] == '100 latest song liked':
            playlistID = item['id']
    return playlistID

playlistID = findThePlaylist(playlists)

def deleteAllTheSongFromThePlaylist(playlistID, username):
    playlistTracks = sp.playlist_tracks(playlistID, fields='items.track')
    for item in playlistTracks['items']:
        trackID = item['track']['uri']
        sp.user_playlist_remove_all_occurrences_of_tracks(username, playlistID, trackID)

delete = deleteAllTheSongFromThePlaylist(playlistID, username)

and here is the error message I receive:

HTTP Error for DELETE to https://api.spotify.com/v1/users/.../playlists/.../tracks returned 405 due to error
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/spotipy/client.py", line 172, in _internal_call
    response.raise_for_status()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: https://api.spotify.com/v1/users/.../playlists/.../tracks

All the process for the credentials seemed to work fine cause the all script is working until the very last line above :

sp.user_playlist_remove_all_occurrences_of_tracks(username, playlistID, trackID)

So it seems that I can access everything but just not interact with it... could it be an issue link to the Authorization Flows? What I understand is that the Authorization Code is giving full access, is it what the prompt_for_user_token is doing?

I can see some similar issues but I couldn't find a way to make it works when following the indications given...

I believe that I'm using the Spotipy version 2.12.0 and python 3.8

Thanks in advance for anyone help!

Originally posted by @dursab in https://github.com/plamere/spotipy/issues/380#issuecomment-632983567

Originally created by @dursab on GitHub (May 30, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/505 Hello, I am trying to delete songs from a playlist with Spotipy and I end up getting the same error as several peoples described in other issues : **http status: 405, code:-1** I tried many adjustments in my script following what had been said in these other issues but no luck so far. Here is the code that I'm using: ``` import os import spotipy import spotipy.util as util # Credentials CLIENT_ID = 'XXXXX' CLIENT_SECRET = 'XXXXX' REDIRECT_URI = 'http://localhost:8888/callback' # Scope scope = 'user-library-read playlist-modify-public playlist-modify-private' # User Id : username = 'XXXXX' # Erase cache and prompt for user permission try : token = util.prompt_for_user_token(username, scope, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI, cache_path=".cache-{username}") print ("SUCCESS!") except: os.remove(f".cache-{username}") token = util.prompt_for_user_token(username, scope, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI, cache_path=".cache-{username}") # Create the spotifyObject sp = spotipy.Spotify(auth=token) # Access to Playlists playlists = sp.current_user_playlists() def findThePlaylist(playlists): playlistID = '' for i, item in enumerate(playlists['items']): if item['name'] == '100 latest song liked': playlistID = item['id'] return playlistID playlistID = findThePlaylist(playlists) def deleteAllTheSongFromThePlaylist(playlistID, username): playlistTracks = sp.playlist_tracks(playlistID, fields='items.track') for item in playlistTracks['items']: trackID = item['track']['uri'] sp.user_playlist_remove_all_occurrences_of_tracks(username, playlistID, trackID) delete = deleteAllTheSongFromThePlaylist(playlistID, username) ``` and here is the error message I receive: ``` HTTP Error for DELETE to https://api.spotify.com/v1/users/.../playlists/.../tracks returned 405 due to error Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/spotipy/client.py", line 172, in _internal_call response.raise_for_status() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: https://api.spotify.com/v1/users/.../playlists/.../tracks ``` All the process for the credentials seemed to work fine cause the all script is working until the very last line above : > sp.user_playlist_remove_all_occurrences_of_tracks(username, playlistID, trackID) So it seems that I can access everything but just not interact with it... could it be an issue link to the **Authorization Flows**? What I understand is that the **Authorization Code** is giving full access, is it what the **prompt_for_user_token** is doing? I can see some similar issues but I couldn't find a way to make it works when following the indications given... I believe that I'm using the Spotipy version 2.12.0 and python 3.8 Thanks in advance for anyone help! _Originally posted by @dursab in https://github.com/plamere/spotipy/issues/380#issuecomment-632983567_
kerem 2026-02-27 23:21:53 +03:00
Author
Owner

@IdmFoundInHim commented on GitHub (Jun 19, 2020):

@dursab
Your problem may be that you are passing a string as trackID when the method expects a list

<!-- gh-comment-id:646875936 --> @IdmFoundInHim commented on GitHub (Jun 19, 2020): @dursab Your problem may be that you are passing a string as `trackID` when the method expects a list
Author
Owner

@dursab commented on GitHub (Jun 21, 2020):

Hello @IdmFoundInHim

I just tried this :

listTrack = []
playlistTracks = sp.playlist_tracks(playlistID, fields='items.track')
for item in playlistTracks['items']:
    trackID = item['track']['uri']
    listTrack.append(trackID)

sp.user_playlist_remove_all_occurrences_of_tracks(username, playlistID, listTrack)

so my now listTrack is a list of all the track I wish to delete, but I get the exact same error as before...
Is that what you were thinking or did I misunderstood?

<!-- gh-comment-id:647187701 --> @dursab commented on GitHub (Jun 21, 2020): Hello @IdmFoundInHim I just tried this : ``` listTrack = [] playlistTracks = sp.playlist_tracks(playlistID, fields='items.track') for item in playlistTracks['items']: trackID = item['track']['uri'] listTrack.append(trackID) sp.user_playlist_remove_all_occurrences_of_tracks(username, playlistID, listTrack) ``` so my now `listTrack` is a list of all the track I wish to delete, but I get the exact same error as before... Is that what you were thinking or did I misunderstood?
Author
Owner

@stephanebruckert commented on GitHub (Jun 21, 2020):

I think that your username is wrong, it should not be your nickname e.g. dursab but instead it should be your ID and should look like a weird sequence of characters e.g. hecpfc6jc5zbm5330hsx9e0rs

Once auth is successful and just before calling user_playlist_remove_all_occurrences_of_tracks, get your username using:

username = sp.me()['id']

Here is a full example https://github.com/plamere/spotipy/blob/master/examples/remove_specific_tracks_from_playlist.py

<!-- gh-comment-id:647190610 --> @stephanebruckert commented on GitHub (Jun 21, 2020): I think that your username is wrong, it should not be your nickname e.g. `dursab` but instead it should be your ID and should look like a weird sequence of characters e.g. `hecpfc6jc5zbm5330hsx9e0rs` Once auth is successful and just before calling `user_playlist_remove_all_occurrences_of_tracks`, get your username using: username = sp.me()['id'] Here is a full example https://github.com/plamere/spotipy/blob/master/examples/remove_specific_tracks_from_playlist.py
Author
Owner

@dursab commented on GitHub (Jun 22, 2020):

Thanks @stephanebruckert, everything is working now.

So the thing is my username was actually a mix between nickname and sequence of characters from the start xxxx?si=jkfninewKFJdsjdso55f665
but when I actually use the command line you suggested username = sp.me()['id'] it's actually returning just the nickname part xxxx.

Thanks a lot for your help.

<!-- gh-comment-id:647478951 --> @dursab commented on GitHub (Jun 22, 2020): Thanks @stephanebruckert, everything is working now. So the thing is my username was actually a mix between nickname and sequence of characters from the start `xxxx?si=jkfninewKFJdsjdso55f665` but when I actually use the command line you suggested `username = sp.me()['id']` it's actually returning just the nickname part `xxxx`. Thanks a lot for your help.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#301
No description provided.