[GH-ISSUE #724] Error when trying to delete song from playlist using SpotifyOAuth #433

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

Originally created by @flancast90 on GitHub (Aug 31, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/724

Hello, when trying to run the code below, I encounter an error:

playlist_id = 'playlist id here'
song_id = 'id of song to remove'
index = 0
sp.playlist_remove_specific_occurrences_of_items(playlist_id, [{"uri":song_id,"positions":[index]}], snapshot_id=None)

I am authenticating as suggested in the documentation as follows:

scope = "playlist-modify-public playlist-modify-private"

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(username='USERNAME', client_id="MY CLIENT ID", client_secret="MY CLIENT SECRET", redirect_uri="https://finnsoftware.net", scope=scope))

The error is as follows:

Traceback (most recent call last):
  File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 559, in get_access_token
    response.raise_for_status()
  File "/home/flanc/.local/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/token

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "SqeaPyCleanSpotify.py", line 104, in <module>
    require_approval()
  File "SqeaPyCleanSpotify.py", line 95, in require_approval
    get_user_action()
  File "SqeaPyCleanSpotify.py", line 45, in get_user_action
    clean_playlist_from_uri(playlist_id)
  File "SqeaPyCleanSpotify.py", line 50, in clean_playlist_from_uri
    playlist = sp.playlist(playlist_id, fields=None, market=None, additional_types=('track', ))
  File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/client.py", line 617, in playlist
    return self._get(
  File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/client.py", line 221, in _internal_call
    headers = self._auth_headers()
  File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/client.py", line 212, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
  File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 565, in get_access_token
    self._handle_oauth_error(http_error)
  File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 146, in _handle_oauth_error
    raise SpotifyOauthError(
spotipy.oauth2.SpotifyOauthError: error: invalid_request, error_description: code must be supplied

Can someone explain what I am doing wrong?

Originally created by @flancast90 on GitHub (Aug 31, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/724 Hello, when trying to run the code below, I encounter an error: ```python playlist_id = 'playlist id here' song_id = 'id of song to remove' index = 0 sp.playlist_remove_specific_occurrences_of_items(playlist_id, [{"uri":song_id,"positions":[index]}], snapshot_id=None) ``` I am authenticating as suggested in the documentation as follows: ```python scope = "playlist-modify-public playlist-modify-private" sp = spotipy.Spotify(auth_manager=SpotifyOAuth(username='USERNAME', client_id="MY CLIENT ID", client_secret="MY CLIENT SECRET", redirect_uri="https://finnsoftware.net", scope=scope)) ``` The error is as follows: ```python Traceback (most recent call last): File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 559, in get_access_token response.raise_for_status() File "/home/flanc/.local/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/token During handling of the above exception, another exception occurred: Traceback (most recent call last): File "SqeaPyCleanSpotify.py", line 104, in <module> require_approval() File "SqeaPyCleanSpotify.py", line 95, in require_approval get_user_action() File "SqeaPyCleanSpotify.py", line 45, in get_user_action clean_playlist_from_uri(playlist_id) File "SqeaPyCleanSpotify.py", line 50, in clean_playlist_from_uri playlist = sp.playlist(playlist_id, fields=None, market=None, additional_types=('track', )) File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/client.py", line 617, in playlist return self._get( File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/client.py", line 297, in _get return self._internal_call("GET", url, payload, kwargs) File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/client.py", line 221, in _internal_call headers = self._auth_headers() File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/client.py", line 212, in _auth_headers token = self.auth_manager.get_access_token(as_dict=False) File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 565, in get_access_token self._handle_oauth_error(http_error) File "/home/flanc/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 146, in _handle_oauth_error raise SpotifyOauthError( spotipy.oauth2.SpotifyOauthError: error: invalid_request, error_description: code must be supplied ``` Can someone explain what I am doing wrong?
kerem 2026-02-27 23:22:37 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@Peter-Schorn commented on GitHub (Aug 31, 2021):

When you run this script, it should ask you to open a URL in your browser. When you open this URL, you should log in and then click the "agree" button. You should then be redirected to a URL that begins with your redirect URI (https://finnsoftware.net). Then, you should paste this URL back into the program in order to complete the authorization process. Is this what you're doing?

<!-- gh-comment-id:909487814 --> @Peter-Schorn commented on GitHub (Aug 31, 2021): When you run this script, it should ask you to open a URL in your browser. When you open this URL, you should log in and then click the "agree" button. You should then be redirected to a URL that begins with your redirect URI (`https://finnsoftware.net`). Then, you should paste this URL back into the program in order to complete the authorization process. Is this what you're doing?
Author
Owner

@flancast90 commented on GitHub (Aug 31, 2021):

Yes, @Peter-Schorn. I'm changing the redirect_url now, because I think javascript window.open() calls from the website are impacting the spotify redirect_url.

From what I read in the docs, it seems that you must include this redirect. Is there a way to bypass that redirect/url entry?

<!-- gh-comment-id:909505522 --> @flancast90 commented on GitHub (Aug 31, 2021): Yes, @Peter-Schorn. I'm changing the redirect_url now, because I think javascript ```window.open()``` calls from the website are impacting the spotify redirect_url. From what I read in the docs, it seems that you must include this redirect. Is there a way to bypass that redirect/url entry?
Author
Owner

@Peter-Schorn commented on GitHub (Aug 31, 2021):

From what I read in the docs, it seems that you must include this redirect. Is there a way to bypass that redirect/url entry?

No, you cannot bypass this. You can read about the authorization process in detail here.

Are you running this as a local command-line script? If so, consider changing the redirect URI to http://localhost:8080.

<!-- gh-comment-id:909515338 --> @Peter-Schorn commented on GitHub (Aug 31, 2021): > From what I read in the docs, it seems that you must include this redirect. Is there a way to bypass that redirect/url entry? No, you cannot bypass this. You can read about the authorization process in detail [here](https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow). Are you running this as a local command-line script? If so, consider changing the redirect URI to `http://localhost:8080`.
Author
Owner

@flancast90 commented on GitHub (Aug 31, 2021):

Thanks for your help, @Peter-Schorn, the issue was window.open() calls in the website js. As soon as I changed the authentication page to about:blank, it started working.

<!-- gh-comment-id:909699226 --> @flancast90 commented on GitHub (Aug 31, 2021): Thanks for your help, @Peter-Schorn, the issue was window.open() calls in the website js. As soon as I changed the authentication page to about:blank, it started working.
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#433
No description provided.