[GH-ISSUE #716] can't add a track to a playlist error 405 #429

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

Originally created by @Yousufakhan95 on GitHub (Aug 13, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/716

ok so I am trying to add a track to my playlist using Spotipy. and I am providing all the right parameters so I don't get what I am doing wrong here and I am getting an error 405 which I don't know how to handle.

class SpotifyAPI(object):
    scope = ['playlist-modify-public','playlist-read-collaborative','playlist-modify-private', 'user-library-read']
    client_id = None
    client_secret = None
    redirect_uri = None

    def __init__(self, client_id, client_secret, redirect_uri, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.client_id = client_id
        self.client_secret = client_secret
        self.redirect_uri = redirect_uri

    def get_user_auth(self):
        mybot = spotipy.SpotifyOAuth(
            client_id = self.client_id, 
            client_secret = self.client_secret,
            redirect_uri = self.redirect_uri,
            scope = self.scope
            )

        token_dict = mybot.get_access_token()
        access_token = token_dict['access_token']

        spotify_object = spotipy.Spotify(auth=access_token)
        return spotify_object

    def search(self, query, search_type):
        spot_object = self.get_user_auth()
        response = spot_object.search(query, limit=1, offset=0, type=search_type, market=None)
        return {'track_link': response['tracks']['items'][0]['external_urls']['spotify'],
        'track_id': response['tracks']['items'][0]['id']}

    def make_playlist(self, user_id, name, description):
        #user_id is username if you dont have your facebook account linked.
        spot_object = self.get_user_auth()
        response = spot_object.user_playlist_create(user = user_id, name = name, public = True, collaborative= False, description = description)

    def add_track_to_given_playlist(self, user, tracks, playlist_url):
        #https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0
        #playlist url needs to be provieded
        #track url needs to be provided
        playlist_id = playlist_url.replace('https://open.spotify.com/playlist/', '')
        spot_object = self.get_user_auth()
        response = spot_object.user_playlist_add_tracks(user = user, playlist_id = playlist_id, tracks = tracks, position = None)


spot = SpotifyAPI(client_id = client_id, client_secret = client_secret,redirect_uri = redirect_uri)


spot.add_track_to_given_playlist(user = username ,playlist_id = 'https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0', tracks = 'https://open.spotify.com/track/6ocbgoVGwYJhOv1GgI9NsF?si=a24899c877e24b0e')`

so if you take a look at the add_track_to_given_playlist method its supposed to add a track to my playlist but its throwing the following error at me.

DeprecationWarning: You're using 'as_dict = True'.get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead.
  token_dict = mybot.get_access_token()
HTTP Error for POST to https://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0/tracks returned 405 due to error
Traceback (most recent call last):
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/requests/models.py", line 953, 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/playlists/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0/tracks

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 60, in <module>
    spot.add_track_to_given_playlist(user = username ,playlist_url = 'https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0', tracks = '6ocbgoVGwYJhOv1GgI9NsF?si=a24899c877e24b0e')
  File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 54, in add_track_to_given_playlist
    response = spot_object.user_playlist_add_tracks(user = user, playlist_id = playlist_id, tracks = tracks, position = None)
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 835, in user_playlist_add_tracks
    return self.playlist_add_items(playlist_id, tracks, position)
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 1025, in playlist_add_items
    return self._post(
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 296, in _post
    return self._internal_call("POST", url, payload, kwargs)
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 261, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 405, code:-1 - https://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0/tracks:
 error, reason: None

thanks

Originally created by @Yousufakhan95 on GitHub (Aug 13, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/716 **ok so I am trying to add a track to my playlist using Spotipy. and I am providing all the right parameters so I don't get what I am doing wrong here and I am getting an error 405 which I don't know how to handle.** ```py class SpotifyAPI(object): scope = ['playlist-modify-public','playlist-read-collaborative','playlist-modify-private', 'user-library-read'] client_id = None client_secret = None redirect_uri = None def __init__(self, client_id, client_secret, redirect_uri, *args, **kwargs): super().__init__(*args, **kwargs) self.client_id = client_id self.client_secret = client_secret self.redirect_uri = redirect_uri def get_user_auth(self): mybot = spotipy.SpotifyOAuth( client_id = self.client_id, client_secret = self.client_secret, redirect_uri = self.redirect_uri, scope = self.scope ) token_dict = mybot.get_access_token() access_token = token_dict['access_token'] spotify_object = spotipy.Spotify(auth=access_token) return spotify_object def search(self, query, search_type): spot_object = self.get_user_auth() response = spot_object.search(query, limit=1, offset=0, type=search_type, market=None) return {'track_link': response['tracks']['items'][0]['external_urls']['spotify'], 'track_id': response['tracks']['items'][0]['id']} def make_playlist(self, user_id, name, description): #user_id is username if you dont have your facebook account linked. spot_object = self.get_user_auth() response = spot_object.user_playlist_create(user = user_id, name = name, public = True, collaborative= False, description = description) def add_track_to_given_playlist(self, user, tracks, playlist_url): #https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0 #playlist url needs to be provieded #track url needs to be provided playlist_id = playlist_url.replace('https://open.spotify.com/playlist/', '') spot_object = self.get_user_auth() response = spot_object.user_playlist_add_tracks(user = user, playlist_id = playlist_id, tracks = tracks, position = None) spot = SpotifyAPI(client_id = client_id, client_secret = client_secret,redirect_uri = redirect_uri) spot.add_track_to_given_playlist(user = username ,playlist_id = 'https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0', tracks = 'https://open.spotify.com/track/6ocbgoVGwYJhOv1GgI9NsF?si=a24899c877e24b0e')` ``` **so if you take a look at the `add_track_to_given_playlist` method its supposed to add a track to my playlist but its throwing the following error at me.** ```bash DeprecationWarning: You're using 'as_dict = True'.get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead. token_dict = mybot.get_access_token() HTTP Error for POST to https://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0/tracks returned 405 due to error Traceback (most recent call last): File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 245, in _internal_call response.raise_for_status() File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/requests/models.py", line 953, 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/playlists/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0/tracks During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 60, in <module> spot.add_track_to_given_playlist(user = username ,playlist_url = 'https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0', tracks = '6ocbgoVGwYJhOv1GgI9NsF?si=a24899c877e24b0e') File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 54, in add_track_to_given_playlist response = spot_object.user_playlist_add_tracks(user = user, playlist_id = playlist_id, tracks = tracks, position = None) File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 835, in user_playlist_add_tracks return self.playlist_add_items(playlist_id, tracks, position) File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 1025, in playlist_add_items return self._post( File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 296, in _post return self._internal_call("POST", url, payload, kwargs) File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 261, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 405, code:-1 - https://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0/tracks: error, reason: None ``` **thanks**
kerem 2026-02-27 23:22:34 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

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

There's a bug with your code that parses the playlist id from the URL. Look very closely at the URL:

https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0

Notice it contains a query parameter si with a value of 99f6cc248a4746a0. This is not part of the id. The id is 5x68eFlK6lB8oN7wAcAL1Z.

Furthermore, there are issues with your authorization code. It doesn't make sense to re-authorize your app before every request. That's just a waste of time. Get rid of the get_user_auth method and change the initializer to the following:

def __init__(self, client_id, client_secret, redirect_uri, *args, **kwargs):
    self.client_id = client_id
    self.client_secret = client_secret
    self.redirect_uri = redirect_uri
    auth_amanger = spotipy.SpotifyOAuth(
        client_id = self.client_id,
        client_secret = self.client_secret,
        redirect_uri = self.redirect_uri,
        scope = self.scope
    )
    self.spot_object = spotipy.Spotify(auth_manager=auth_amanger)

Use the spot_object instance attribute for all requests to spotify.

<!-- gh-comment-id:898697664 --> @Peter-Schorn commented on GitHub (Aug 13, 2021): There's a bug with your code that parses the playlist id from the URL. Look very closely at the URL: > https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0 Notice it contains a query parameter `si` with a value of `99f6cc248a4746a0`. This is not part of the id. The id is `5x68eFlK6lB8oN7wAcAL1Z`. Furthermore, there are issues with your authorization code. It doesn't make sense to re-authorize your app before every request. That's just a waste of time. Get rid of the `get_user_auth` method and change the initializer to the following: ```python def __init__(self, client_id, client_secret, redirect_uri, *args, **kwargs): self.client_id = client_id self.client_secret = client_secret self.redirect_uri = redirect_uri auth_amanger = spotipy.SpotifyOAuth( client_id = self.client_id, client_secret = self.client_secret, redirect_uri = self.redirect_uri, scope = self.scope ) self.spot_object = spotipy.Spotify(auth_manager=auth_amanger) ``` Use the `spot_object` instance attribute for all requests to spotify.
Author
Owner

@Yousufakhan95 commented on GitHub (Aug 14, 2021):

ok thanks for the advice on the auth and the method. the auth still works and now the code looks better too! but the problem is still here. instead of getting error 405 now I am getting error 400 which is a little better but I can't seem to get the problem here again. here is the revised code with the error message.

class SpotifyAPI(object):
    scope = ['playlist-modify-public','playlist-read-collaborative','playlist-modify-private', 'user-library-read']

    def __init__(self, client_id, client_secret, redirect_uri, *args, **kwargs):
        self.client_id = client_id
        self.client_secret = client_secret
        self.redirect_uri = redirect_uri
        auth_manager = spotipy.SpotifyOAuth(
            client_id = self.client_id,
            client_secret = self.client_secret,
            redirect_uri = self.redirect_uri,
            scope = self.scope
        )
        self.spot_object = spotipy.Spotify(auth_manager=auth_manager)

    def search(self, query, search_type):
        response = self.spot_object.search(query, limit=1, offset=0, type=search_type, market=None)
        return {'track_link': response['tracks']['items'][0]['external_urls']['spotify'],
        'track_id': response['tracks']['items'][0]['id']}

    def make_playlist(self, user_id, name, description):
        #user_id is username if you dont have your facebook account linked.
        response = self.spot_object.user_playlist_create(user = user_id, name = name, public = True, collaborative= False, description = description)

    def add_track_to_given_playlist(self, user, items, playlist_url):
        #playlist url needs to be provieded
        #track url needs to be provided
        playlist_id = playlist_url.replace('https://open.spotify.com/playlist/', '')
        # this for loop thing works there are no problems here VV
        for index,chars in enumerate(playlist_id):
            if playlist_id[index] == '?' and playlist_id[index+1] == 's' and playlist_id[index+2] == 'i':
                playlist_id = playlist_id[:index]
                break
        response = self.spot_object.playlist_add_items(playlist_id = playlist_id, items = items, position = 1)

and here is the error message. reminder this is for the add_track_to_given_playlist method

HTTP Error for POST to https://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z/tracks returned 400 due to Invalid track uri: spotify:track:h
Traceback (most recent call last):
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/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://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z/tracks?position=1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 49, in <module>
    spot.add_track_to_given_playlist(user = username ,playlist_url = 'https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0', items = 'https://open.spotify.com/track/35mvY5S1H3J2QZyna3TFe0?si=c630ecfac8804558')
  File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 42, in add_track_to_given_playlist
    response = self.spot_object.playlist_add_items(playlist_id = playlist_id, items = items, position = 1)
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 1025, in playlist_add_items
    return self._post(
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 296, in _post
    return self._internal_call("POST", url, payload, kwargs)
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 261, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z/tracks?position=1:
 Invalid track uri: spotify:track:h, reason: None```
<!-- gh-comment-id:898810159 --> @Yousufakhan95 commented on GitHub (Aug 14, 2021): **ok thanks for the advice on the auth and the method. the auth still works and now the code looks better too! but the problem is still here. instead of getting error 405 now I am getting error 400 which is a little better but I can't seem to get the problem here again. here is the revised code with the error message.** ```py class SpotifyAPI(object): scope = ['playlist-modify-public','playlist-read-collaborative','playlist-modify-private', 'user-library-read'] def __init__(self, client_id, client_secret, redirect_uri, *args, **kwargs): self.client_id = client_id self.client_secret = client_secret self.redirect_uri = redirect_uri auth_manager = spotipy.SpotifyOAuth( client_id = self.client_id, client_secret = self.client_secret, redirect_uri = self.redirect_uri, scope = self.scope ) self.spot_object = spotipy.Spotify(auth_manager=auth_manager) def search(self, query, search_type): response = self.spot_object.search(query, limit=1, offset=0, type=search_type, market=None) return {'track_link': response['tracks']['items'][0]['external_urls']['spotify'], 'track_id': response['tracks']['items'][0]['id']} def make_playlist(self, user_id, name, description): #user_id is username if you dont have your facebook account linked. response = self.spot_object.user_playlist_create(user = user_id, name = name, public = True, collaborative= False, description = description) def add_track_to_given_playlist(self, user, items, playlist_url): #playlist url needs to be provieded #track url needs to be provided playlist_id = playlist_url.replace('https://open.spotify.com/playlist/', '') # this for loop thing works there are no problems here VV for index,chars in enumerate(playlist_id): if playlist_id[index] == '?' and playlist_id[index+1] == 's' and playlist_id[index+2] == 'i': playlist_id = playlist_id[:index] break response = self.spot_object.playlist_add_items(playlist_id = playlist_id, items = items, position = 1) ``` **and here is the error message. reminder this is for the `add_track_to_given_playlist` method** ```bash HTTP Error for POST to https://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z/tracks returned 400 due to Invalid track uri: spotify:track:h Traceback (most recent call last): File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 245, in _internal_call response.raise_for_status() File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/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://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z/tracks?position=1 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 49, in <module> spot.add_track_to_given_playlist(user = username ,playlist_url = 'https://open.spotify.com/playlist/5x68eFlK6lB8oN7wAcAL1Z?si=99f6cc248a4746a0', items = 'https://open.spotify.com/track/35mvY5S1H3J2QZyna3TFe0?si=c630ecfac8804558') File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 42, in add_track_to_given_playlist response = self.spot_object.playlist_add_items(playlist_id = playlist_id, items = items, position = 1) File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 1025, in playlist_add_items return self._post( File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 296, in _post return self._internal_call("POST", url, payload, kwargs) File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 261, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/5x68eFlK6lB8oN7wAcAL1Z/tracks?position=1: Invalid track uri: spotify:track:h, reason: None```
Author
Owner

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

The items parameter of playlist_add_items expects a list of tracks/episodes, not a single track. You should be able to tell that based on that fact that items is plural. Also, you should read the documentation.

<!-- gh-comment-id:898812510 --> @Peter-Schorn commented on GitHub (Aug 14, 2021): The `items` parameter of `playlist_add_items` expects a list of tracks/episodes, not a single track. You should be able to tell that based on that fact that `items` is plural. Also, you should _read the documentation_.
Author
Owner

@Yousufakhan95 commented on GitHub (Aug 14, 2021):

oh ok thanks. sorry about that. is there a way I can just give it only one song to add. or can I just put one song url in the list and give it that. sorry to bother you this is my first time using an api.

<!-- gh-comment-id:898929422 --> @Yousufakhan95 commented on GitHub (Aug 14, 2021): oh ok thanks. sorry about that. is there a way I can just give it only one song to add. or can I just put one song url in the list and give it that. sorry to bother you this is my first time using an api.
Author
Owner

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

You can always create a list with a single element.

<!-- gh-comment-id:898932565 --> @Peter-Schorn commented on GitHub (Aug 14, 2021): You can always create a list with a single element.
Author
Owner

@Yousufakhan95 commented on GitHub (Aug 14, 2021):

ok just tried that and now error 403 here is what I am passing in.

    def add_track_to_given_playlist(self, items, playlist_url):
        #playlist url needs to be provieded
        #track url needs to be provided
        playlist_id = playlist_url.replace('https://open.spotify.com/playlist/', '')
        for index,chars in enumerate(playlist_id):
            if playlist_id[index] == '?' and playlist_id[index+1] == 's' and playlist_id[index+2] == 'i':
                playlist_id = playlist_id[:index]
                break
        response = self.spot_object.playlist_add_items(playlist_id = playlist_id, items = items, position = 1)


spot = SpotifyAPI(client_id = client_id, client_secret = client_secret,redirect_uri = redirect_uri)

spot.add_track_to_given_playlist(playlist_url = 'https://open.spotify.com/playlist/7cPvH0M6or3YVglGnCPi5h?si=9e467138f2974c17', items = ['https://open.spotify.com/track/35mvY5S1H3J2QZyna3TFe0?si=c630ecfac8804558'])

and here is the error message

HTTP Error for POST to https://api.spotify.com/v1/playlists/7cPvH0M6or3YVglGnCPi5h/tracks returned 403 due to Index out of bounds.
Traceback (most recent call last):
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.spotify.com/v1/playlists/7cPvH0M6or3YVglGnCPi5h/tracks?position=1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 49, in <module>
    spot.add_track_to_given_playlist(user = username ,playlist_url = 'https://open.spotify.com/playlist/7cPvH0M6or3YVglGnCPi5h?si=9e467138f2974c17', items = ['https://open.spotify.com/track/35mvY5S1H3J2QZyna3TFe0?si=c630ecfac8804558'])
  File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 42, in add_track_to_given_playlist
    response = self.spot_object.playlist_add_items(playlist_id = playlist_id, items = items, position = 1)
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 1025, in playlist_add_items
    return self._post(
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 296, in _post
    return self._internal_call("POST", url, payload, kwargs)
  File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 261, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/playlists/7cPvH0M6or3YVglGnCPi5h/tracks?position=1:
 Index out of bounds., reason: None

which scopes should I use?

<!-- gh-comment-id:898932961 --> @Yousufakhan95 commented on GitHub (Aug 14, 2021): **ok just tried that and now error 403 here is what I am passing in.** ```py def add_track_to_given_playlist(self, items, playlist_url): #playlist url needs to be provieded #track url needs to be provided playlist_id = playlist_url.replace('https://open.spotify.com/playlist/', '') for index,chars in enumerate(playlist_id): if playlist_id[index] == '?' and playlist_id[index+1] == 's' and playlist_id[index+2] == 'i': playlist_id = playlist_id[:index] break response = self.spot_object.playlist_add_items(playlist_id = playlist_id, items = items, position = 1) spot = SpotifyAPI(client_id = client_id, client_secret = client_secret,redirect_uri = redirect_uri) spot.add_track_to_given_playlist(playlist_url = 'https://open.spotify.com/playlist/7cPvH0M6or3YVglGnCPi5h?si=9e467138f2974c17', items = ['https://open.spotify.com/track/35mvY5S1H3J2QZyna3TFe0?si=c630ecfac8804558']) ``` **and here is the error message** ```bash HTTP Error for POST to https://api.spotify.com/v1/playlists/7cPvH0M6or3YVglGnCPi5h/tracks returned 403 due to Index out of bounds. Traceback (most recent call last): File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 245, in _internal_call response.raise_for_status() File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.spotify.com/v1/playlists/7cPvH0M6or3YVglGnCPi5h/tracks?position=1 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 49, in <module> spot.add_track_to_given_playlist(user = username ,playlist_url = 'https://open.spotify.com/playlist/7cPvH0M6or3YVglGnCPi5h?si=9e467138f2974c17', items = ['https://open.spotify.com/track/35mvY5S1H3J2QZyna3TFe0?si=c630ecfac8804558']) File "/Users/yousufahmedkhan/Documents/coding/personal-projects/spotify api/spotify_client_3_with_spotipy.py", line 42, in add_track_to_given_playlist response = self.spot_object.playlist_add_items(playlist_id = playlist_id, items = items, position = 1) File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 1025, in playlist_add_items return self._post( File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 296, in _post return self._internal_call("POST", url, payload, kwargs) File "/Users/yousufahmedkhan/.local/share/virtualenvs/spotify_api-qzww0aDl/lib/python3.8/site-packages/spotipy/client.py", line 261, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/playlists/7cPvH0M6or3YVglGnCPi5h/tracks?position=1: Index out of bounds., reason: None ``` **which scopes should I use?**
Author
Owner

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

The position parameter should be 0 because the playlist is empty. lists are zero-indexed, which means the index of the first element is zero. Once again, read the documentation.

<!-- gh-comment-id:898934471 --> @Peter-Schorn commented on GitHub (Aug 14, 2021): The `position` parameter should be `0` because the playlist is empty. lists are zero-indexed, which means the index of the first element is zero. Once again, read the [documentation](https://developer.spotify.com/documentation/web-api/reference/#endpoint-add-tracks-to-playlist).
Author
Owner

@Yousufakhan95 commented on GitHub (Aug 14, 2021):

omg thanks it works now. sorry that I was bugging you a-lot. thanks so much. this is why I love the programming community guys like you have made it much easier to get into programming. I wish to help people join this community in the future just like you helped me. thanks a lot man

<!-- gh-comment-id:898934935 --> @Yousufakhan95 commented on GitHub (Aug 14, 2021): omg thanks it works now. sorry that I was bugging you a-lot. thanks so much. this is why I love the programming community guys like you have made it much easier to get into programming. I wish to help people join this community in the future just like you helped me. thanks a lot man
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#429
No description provided.