[GH-ISSUE #512] Error 404 #307

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

Originally created by @hafraz07 on GitHub (Jun 13, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/512

import sys
import spotipy
import spotipy.util as util
from datetime import date

CLIENT_ID = ''
CLIENT_SECRET = ''
REDIRECT_URI = ''

def create_playlist(res, sp_obj, user):
    playlist_name = date.today().strftime('%m-%d')
    # Checks if playlist already exists
    playlists = sp_obj.user_playlists(user)
    for playlist in playlists['items']:
        if playlist['name'] == playlist_name:
            print('Playlist', playlist_name, 'already exists')
            return
    # Adds top tracks to track_ids
    track_ids = []
    for i, item in enumerate(res['items']):
        track_ids.append(item['track']['id'])

    # Creates playlist with name MM-DD
    sp_obj.user_playlist_create(user, playlist_name)

    # Gets playlist id of created playlist
    playlists = sp_obj.user_playlists(user)
    for playlist in playlists['items']:
        if playlist['name'] == playlist_name:
            playlist_id = playlist['id']
            break
    # Adds 50 recently played tracks to the playlist
    sp_obj.user_playlist_add_tracks(user, playlist_id, track_ids)
    print('Successfully created playlist', playlist_name)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        username = sys.argv[1]
    else:
        print("Whoops, need your username!")
        print("usage: python playlist_generator.py [username]")
        sys.exit()

    scopes = 'playlist-modify-public user-top-read user-read-recently-played'

    token = util.prompt_for_user_token(username, scopes, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI)

    if token:
        sp = spotipy.Spotify(auth=token)
        results = sp.current_user_recently_played(limit=50)
        create_playlist(results, sp, username)
    else:
        print("Can't get token for", username)

Hi, I made this script to create a playlist based on my recently played tracks. This works for my own account but currently gets a 404 when my friend tries to run the script with his account. I don't know if I'm doing something wrong or if this is an issue with the library. Thanks

Originally created by @hafraz07 on GitHub (Jun 13, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/512 ``` import sys import spotipy import spotipy.util as util from datetime import date CLIENT_ID = '' CLIENT_SECRET = '' REDIRECT_URI = '' def create_playlist(res, sp_obj, user): playlist_name = date.today().strftime('%m-%d') # Checks if playlist already exists playlists = sp_obj.user_playlists(user) for playlist in playlists['items']: if playlist['name'] == playlist_name: print('Playlist', playlist_name, 'already exists') return # Adds top tracks to track_ids track_ids = [] for i, item in enumerate(res['items']): track_ids.append(item['track']['id']) # Creates playlist with name MM-DD sp_obj.user_playlist_create(user, playlist_name) # Gets playlist id of created playlist playlists = sp_obj.user_playlists(user) for playlist in playlists['items']: if playlist['name'] == playlist_name: playlist_id = playlist['id'] break # Adds 50 recently played tracks to the playlist sp_obj.user_playlist_add_tracks(user, playlist_id, track_ids) print('Successfully created playlist', playlist_name) if __name__ == '__main__': if len(sys.argv) > 1: username = sys.argv[1] else: print("Whoops, need your username!") print("usage: python playlist_generator.py [username]") sys.exit() scopes = 'playlist-modify-public user-top-read user-read-recently-played' token = util.prompt_for_user_token(username, scopes, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI) if token: sp = spotipy.Spotify(auth=token) results = sp.current_user_recently_played(limit=50) create_playlist(results, sp, username) else: print("Can't get token for", username) ``` Hi, I made this script to create a playlist based on my recently played tracks. This works for my own account but currently gets a 404 when my friend tries to run the script with his account. I don't know if I'm doing something wrong or if this is an issue with the library. Thanks
kerem closed this issue 2026-02-27 23:21:55 +03:00
Author
Owner

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

Hi @hafraz07, on which line does your friend get a 404? the stack trace would be helpful also. Thanks

<!-- gh-comment-id:643590596 --> @stephanebruckert commented on GitHub (Jun 13, 2020): Hi @hafraz07, on which line does your friend get a 404? the stack trace would be helpful also. Thanks
Author
Owner

@hafraz07 commented on GitHub (Jun 13, 2020):

HTTP Error for GET to https://api.spotify.com/v1/users/farahzabinrahman/playlists returned 404 due to Not found.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 172, in _internal_call
    response.raise_for_status()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/users/farahzabinrahman/playlists?limit=50&offset=0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "playlist_generator.py", line 62, in <module>
    create_playlist(results, sp, username)
  File "playlist_generator.py", line 17, in create_playlist
    playlists = sp_obj.user_playlists(user)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 609, in user_playlists
    "users/%s/playlists" % user, limit=limit, offset=offset
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 207, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 187, in _internal_call
    headers=response.headers,
spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/users/farahzabinrahman/playlists?limit=50&offset=0:
 Not found.

Here you go

<!-- gh-comment-id:643677236 --> @hafraz07 commented on GitHub (Jun 13, 2020): ``` HTTP Error for GET to https://api.spotify.com/v1/users/farahzabinrahman/playlists returned 404 due to Not found. Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 172, in _internal_call response.raise_for_status() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/users/farahzabinrahman/playlists?limit=50&offset=0 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "playlist_generator.py", line 62, in <module> create_playlist(results, sp, username) File "playlist_generator.py", line 17, in create_playlist playlists = sp_obj.user_playlists(user) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 609, in user_playlists "users/%s/playlists" % user, limit=limit, offset=offset File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 207, in _get return self._internal_call("GET", url, payload, kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 187, in _internal_call headers=response.headers, spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/users/farahzabinrahman/playlists?limit=50&offset=0: Not found. ``` Here you go
Author
Owner

@hafraz07 commented on GitHub (Jun 13, 2020):

It's still asking for a username or cache_path

Traceback (most recent call last):
  File "playlist_generator.py", line 59, in <module>
    results = sp.current_user_recently_played(limit=50)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 946, in current_user_recently_played
    before=before,
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 207, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 151, in _internal_call
    headers = self._auth_headers()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 142, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 405, in get_access_token
    token_info = self.get_cached_token()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 245, in get_cached_token
    "You must either set a cache_path or a username."
spotipy.oauth2.SpotifyOauthError: You must either set a cache_path or a username.
<!-- gh-comment-id:643683629 --> @hafraz07 commented on GitHub (Jun 13, 2020): It's still asking for a username or cache_path ``` Traceback (most recent call last): File "playlist_generator.py", line 59, in <module> results = sp.current_user_recently_played(limit=50) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 946, in current_user_recently_played before=before, File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 207, in _get return self._internal_call("GET", url, payload, kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 151, in _internal_call headers = self._auth_headers() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/client.py", line 142, in _auth_headers token = self.auth_manager.get_access_token(as_dict=False) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 405, in get_access_token token_info = self.get_cached_token() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 245, in get_cached_token "You must either set a cache_path or a username." spotipy.oauth2.SpotifyOauthError: You must either set a cache_path or a username. ```
Author
Owner

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

I'm really not sure why it displays this message, sorry. However this works well for me:

import spotipy
from datetime import date


def create_playlist(res, sp_obj):
    playlist_name = date.today().strftime('%m-%d')
    # Checks if playlist already exists
    playlists = sp_obj.current_user_playlists()
    for playlist in playlists['items']:
        if playlist['name'] == playlist_name:
            print('Playlist', playlist_name, 'already exists')
            return
    # Adds top tracks to track_ids
    track_ids = []
    for i, item in enumerate(res['items']):
        track_ids.append(item['track']['id'])

    user_id = sp.me()['id']
    # Creates playlist with name MM-DD
    sp_obj.user_playlist_create(user_id, playlist_name)

    # Gets playlist id of created playlist
    playlists = sp_obj.current_user_playlists()
    for playlist in playlists['items']:
        if playlist['name'] == playlist_name:
            playlist_id = playlist['id']
            break
    # Adds 50 recently played tracks to the playlist
    sp_obj.user_playlist_add_tracks(user_id, playlist_id, track_ids)
    print('Successfully created playlist', playlist_name)


if __name__ == '__main__':
    scopes = 'playlist-modify-public user-top-read user-read-recently-played'

    auth_manager = spotipy.oauth2.SpotifyOAuth(scope=scopes)
    sp = spotipy.Spotify(client_credentials_manager=auth_manager)
    results = sp.current_user_recently_played(limit=50)
    create_playlist(results, sp)
<!-- gh-comment-id:643685933 --> @stephanebruckert commented on GitHub (Jun 13, 2020): I'm really not sure why it displays this message, sorry. However this works well for me: ```python3 import spotipy from datetime import date def create_playlist(res, sp_obj): playlist_name = date.today().strftime('%m-%d') # Checks if playlist already exists playlists = sp_obj.current_user_playlists() for playlist in playlists['items']: if playlist['name'] == playlist_name: print('Playlist', playlist_name, 'already exists') return # Adds top tracks to track_ids track_ids = [] for i, item in enumerate(res['items']): track_ids.append(item['track']['id']) user_id = sp.me()['id'] # Creates playlist with name MM-DD sp_obj.user_playlist_create(user_id, playlist_name) # Gets playlist id of created playlist playlists = sp_obj.current_user_playlists() for playlist in playlists['items']: if playlist['name'] == playlist_name: playlist_id = playlist['id'] break # Adds 50 recently played tracks to the playlist sp_obj.user_playlist_add_tracks(user_id, playlist_id, track_ids) print('Successfully created playlist', playlist_name) if __name__ == '__main__': scopes = 'playlist-modify-public user-top-read user-read-recently-played' auth_manager = spotipy.oauth2.SpotifyOAuth(scope=scopes) sp = spotipy.Spotify(client_credentials_manager=auth_manager) results = sp.current_user_recently_played(limit=50) create_playlist(results, sp) ```
Author
Owner

@lucas-campbell commented on GitHub (Jun 17, 2020):

I was having the same problem in addition to those mentioned in #503, and only after revoking access to a spotify app, so may be related? In any case, the fix for me was to give a specific cache_path when constructing the SpotifyOAuth object (maybe 'manually' forces a refresh of the token/permission?). E.g:
oauth = spotipy.oauth2.SpotifyOAuth(scope=scope, username=u, cache_path='./tokens.txt') sp = spotipy.Spotify(auth_manager=oauth)

<!-- gh-comment-id:645671145 --> @lucas-campbell commented on GitHub (Jun 17, 2020): I was having the same problem in addition to those mentioned in #503, and only after revoking access to a spotify app, so may be related? In any case, the fix for me was to give a specific cache_path when constructing the SpotifyOAuth object (maybe 'manually' forces a refresh of the token/permission?). E.g: `oauth = spotipy.oauth2.SpotifyOAuth(scope=scope, username=u, cache_path='./tokens.txt') sp = spotipy.Spotify(auth_manager=oauth)`
Author
Owner

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

@hafraz07 did the given solutions help?

<!-- gh-comment-id:651929213 --> @stephanebruckert commented on GitHub (Jun 30, 2020): @hafraz07 did the given solutions help?
Author
Owner

@hafraz07 commented on GitHub (Jul 2, 2020):

I didn't specify a cache path but I specified an arbitrary constant username (username=u) and that seems to work

<!-- gh-comment-id:652761388 --> @hafraz07 commented on GitHub (Jul 2, 2020): I didn't specify a cache path but I specified an arbitrary constant username (username=u) and that seems to work
Author
Owner

@stephanebruckert commented on GitHub (Aug 30, 2020):

Fixed in https://github.com/plamere/spotipy/pull/567

<!-- gh-comment-id:683481696 --> @stephanebruckert commented on GitHub (Aug 30, 2020): Fixed in https://github.com/plamere/spotipy/pull/567
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#307
No description provided.