简体   繁体   中英

cant add tracks to spotify playlist

having an issue with adding tracks to a playlist. i can obtain the currently playing song and create a playlist if it doesnt already exist but once i try to add tracks to the playlist it gives me this error

An error occurred: http status: 403, code:-1 You cannot add tracks to a playlist you don't own., reason: None

def add_artist_songs_to_playlist():
sp_oauth = SpotifyOAuth(client_id, client_secret, redirect_uri,
                        scope="app-remote-control user-library-read user-read-playback-state  user-read-private user-read-email playlist-read-private playlist-modify-public playlist-modify-private",
                        cache_path=".cache-" + username)
spotify_api = spotipy.Spotify(auth_manager=sp_oauth)
artist_name, artist_id = get_artist_info()
try:
    songs = []
    offset = 0
    while True:
        result = spotify_api.artist_albums(artist_id, offset=offset)
        albums = result['items']
        if not albums:
            break
        for album in albums:
            album_tracks = spotify_api.album_tracks(album['id'])
            for track in album_tracks['items']:
                songs.append(track['id'])
        offset += 20
    spotify_api.playlist_add_items(playlist_id, songs)
    print(f"Successfully added {len(songs)} songs to the playlist!")
except spotipy.client.SpotifyException as e:
    print(f"An error occurred: {e}")

How do you get playlist_id ? It is likely not allowing you modify a playlist that does not belong to the authenticated user.

You can try getting the current user's playlists and see if the playlist_id you are trying to modify is listed in there: https://spotipy.readthedocs.io/en/2.22.1/#spotipy.client.Spotify.current_user_playlists

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM