I’m getting a 403 error when trying to create playlists using the Spotify API. The weird thing is my code was working fine earlier today. I just stepped away for a few minutes and when I came back it started failing.
user_id = '8mn6xkqweah21rd3pjx9y5nop'
client_app_id = 'b85eacbd28574df4962cef178e765c54'
client_app_secret = '{My Secret Key}'
redirect_url = 'http://localhost:80'
# Initialize Spotify client with OAuth
spotify_client = spotipy.Spotify(auth_manager=SpotifyOAuth(
client_id=client_app_id,
client_secret=client_app_secret,
redirect_uri=redirect_url,
scope="user-library-read"
))
# Retrieve access token
token_info = spotify_client.auth_manager.get_access_token(as_dict=False)
def generate_recommendation_playlist(content_type, seed_id, list_name='My Recommendations'):
client = spotipy.Spotify(auth=token_info, auth_manager=SpotifyOAuth(client_id=client_app_id,
client_secret=client_app_secret,
redirect_uri=redirect_url,
scope='playlist-modify-public'))
# Fetch recommendations based on seed
if content_type == "song":
recommendations = client.recommendations(seed_tracks=[seed_id], limit=10)['tracks']
elif content_type == "musician":
recommendations = client.recommendations(seed_artists=[seed_id], limit=10)['tracks']
# Extract URIs from recommendations
song_uris = [song['uri'] for song in recommendations]
# Create new playlist
new_playlist = client.user_playlist_create(user=user_id, name=list_name, public=False)
# Add songs to playlist
client.playlist_add_items(playlist_id=new_playlist['id'], items=song_uris)
return new_playlist
# This line triggers the error
my_playlist = generate_recommendation_playlist("musician", "4rbE8MKSOBhgHhGiUVLmX")
The error says insufficient client scope but I have the right scopes set. My other function for getting user’s top tracks works fine with different scopes. I’ve tried recreating the app in Spotify dashboard and using fresh code but nothing works. What am I missing here?