0

Im doing a simple script to update a playlist but I have an error even if everything looks good: `

The script start, I URI works and give me a token, spotify open to asked me the access but after it fail :

   HTTP Error for PUT to https://api.spotify.com/v1/playlists/6rkyVmrfPEeWkJm01mbhO1 with 
   Params: {} returned 401 due to Invalid access token
   Traceback (most recent call last):
   File "C:\Users\xxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 269, in _internal_call
   response.raise_for_status()
   File "C:\Users\xxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 1021, in raise_for_status
  raise HTTPError(http_error_msg, response=self)
  requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: 
  https://api.spotify.com/v1/playlists/6rkyVmrfPEeWkJm01mbhO1

Python code:

def func():
scope = 'playlist-modify-public'
username = 'myusername'
SPOTIPY_CLIENT_ID = 'xxxxxxxxxxxx'
SPOTIPY_CLIENT_SECRET = 'xxxxxxxxx'

token = util.prompt_for_user_token(username,scope,SPOTIPY_CLIENT_ID,SPOTIPY_CLIENT_SECRET,redirect_uri='https://www.dev.com')
print(token)
sp = spotipy.Spotify(auth="token")

id = 'https://open.spotify.com/playlist/6rkyVmrfPEeWkJm01mbhO1'
playlist_name = 'it works'
playlist_description = 'updated by script'
sp.user_playlist_change_details(username, id, name=playlist_name, description=playlist_description)
func()

here another try :

I get :

py spotify_bot.py 
HTTP Error for PUT to https://api.spotify.com/v1/playlists/6rkyVmrfPEeWkJm01mbhO1 with Params: {} returned 403 due to Not allowed
An exception occurred

Here the code

import spotipy
 from spotipy.oauth2 import SpotifyOAuth
 import json

 def func():
     SCOPE = 'playlist-modify-public'
     USER_ID = 'xifeat'
     REDIRECT_URI = 'http://localhost:3000/callback'
     CLIENT_ID = '6cff431d1dea40f4812460b4721032b0'
     CLIENT_SECRET = 'fb0bcffd4b2b41ac84b9b1d7501dbb80'
     PLAYLIST_ID = '6rkyVmrfPEeWkJm01mbhO1'
     PLAYLIST_NAME = 'test'
     DESCRIPTION = 'Updated !'
     try:    
         auth_manager = SpotifyOAuth(
         scope=SCOPE,
        username=USER_ID,
        redirect_uri=REDIRECT_URI,
        client_id=CLIENT_ID,
        client_secret=CLIENT_SECRET)
    spotify = spotipy.Spotify(auth_manager=auth_manager)

    spotify.user_playlist_change_details(
        user=USER_ID,
        playlist_id=PLAYLIST_ID,
        name=PLAYLIST_NAME,
        description=DESCRIPTION)
except:
    print("An exception occurred")
 func()

enter image description here

`

xif
  • 343
  • 1
  • 9
  • 25
  • I recommend you to Rotate your Client Secret in the [Spotify App Dashboard](https://developer.spotify.com/dashboard/applications), because that should stay private. – Ximzend Feb 04 '23 at 19:09
  • Have you tried adding the scope `playlist-modify-private`? – Ximzend Feb 04 '23 at 19:14
  • I checked if your playlist is public whit [the Get Playlist endpoint](https://developer.spotify.com/console/get-playlist/?playlist_id=6rkyVmrfPEeWkJm01mbhO1&market=&fields=public&additional_types=), and it says false (aka not on profile). So, the `playlist-modify-public` scope isn't sufficient, and the `playlist-modify-private` is required. – Ximzend Feb 05 '23 at 07:48

2 Answers2

0

Yes, it is a Scope problem.
The playlist you are trying to change, is private/not public*. That requires the playlist-modify-private scope.

* On Spotify, all playlists that are not displayed on your profile, are considered private/not public.

Ximzend
  • 388
  • 1
  • 4
  • 10
-1

You needs to pass correct parameters in here

The playlist_id just id of playlist, it is not full URL.

    def user_playlist_change_details(
        self,
        user,
        playlist_id,
        name=None,
        public=None,
        collaborative=None,
        description=None,
    ):

    Parameters:
        - user - the id of the user
        - playlist_id - the id of the playlist
        - name - optional name of the playlist
        - public - optional is the playlist public
        - collaborative - optional is the playlist collaborative
        - description - optional description of the playlist

This code will works

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import json

def func():
    SCOPE = ['playlist-modify-private','playlist-modify-public']
    USER_ID = 'your-user-id'
    REDIRECT_URI = 'your-redirect-uri'
    CLIENT_ID = 'your-client-id'
    CLIENT_SECRET = 'your-client-secret'
    PLAYLIST_ID = 'your-play-list'
    PLAYLIST_NAME = 'Oh my new playlist name!'
    DESCRIPTION = 'This is new description'
    try:    
        auth_manager = SpotifyOAuth(
            scope=SCOPE,
            username=USER_ID,
            redirect_uri=REDIRECT_URI,
            client_id=CLIENT_ID,
            client_secret=CLIENT_SECRET)
        spotify = spotipy.Spotify(auth_manager=auth_manager)

        spotify.user_playlist_change_details(
            user=USER_ID,
            playlist_id=PLAYLIST_ID,
            name=PLAYLIST_NAME,
            description=DESCRIPTION)
    except:
        print("An exception occurred")
func()

Before change enter image description here

After change enter image description here

The scope is ['playlist-modify-private','playlist-modify-public']

Bench Vue
  • 5,257
  • 2
  • 10
  • 14
  • Thank you so much for your answer ! so it seems to be better but now I have : Jm01mbhO1 with Params: {} returned 403 due to Insufficient client scope An exception occurred So a scope issue, did I miss something ? I create my app through the developper dashboard, I put the exact same code, the URI is added, the window open I can log, I can get the token, the it fail. There is a particular settings somewhere? – xif Jan 31 '23 at 08:19
  • HTTP error 403 means your token not efficient `playlist-modify-public` scope. Can you try add new App in Dashboard. Try [it again](https://stackoverflow.com/questions/74651517/expo-auth-session-spotify-get-refresh-token/74655243#74655243)? My demo code works. it means not parameters issue. – Bench Vue Jan 31 '23 at 12:25
  • @xif, I made a program can see the scopes of your token. You can see list of scope in [here](https://stackoverflow.com/questions/75286588/spotify-web-api-call-gives-wrong-code-python/75292843#75292843), add your `playlist-modify-public` and remove `playlist-modify-public` then compare it is matter or not. – Bench Vue Jan 31 '23 at 16:43
  • I dont get it I tried 200 times, I delete, recreate the app, I tried with another account, I tried everything. I updated my initial post with the exact code I used, this playlist is just for test... I dont get it, its public, the scope should be good, but it seems I have a token issue. – xif Jan 31 '23 at 20:47
  • I even added multiple users in the users and access setting to try others – xif Jan 31 '23 at 20:48
  • Did you try [Flask](https://stackoverflow.com/questions/75286588/spotify-web-api-call-gives-wrong-code-python/75292843#75292843) example? – Bench Vue Jan 31 '23 at 20:52
  • {"access_token": "BQDvZFaUnEURQuoPdIpWAb2UoJGizFPOflizIXofER4z0uRWocB7DHmdargxnpbYaa1zVsJrDI4nGxjW4LJzX03md7Cq_s0AlSfRnwtifmepdphI92mFzPrs0-gSToocXkxEsoagYgsSUqCpF9fmgazlpUA9iTXAProBNFJJRt6uwpjUSACW0Q", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "AQC9hcJAAEfL93kfpSIkQ5KbrMfxo_OhHaeaJZilCe_tyyJte5V6mdmOsVSR7D7DCcIqqNBBdirjZKJvRS-upxnlmS8hzWIX3tMEc8VS_31QZBpCYir9DMtSJZyIdY4RdZ0", "scope": "playlist-read-collaborative user-read-email"} – xif Jan 31 '23 at 21:30
  • Good Job. now just add one scope, test again. It can get token, no problem that client id and secret. Try it with this scope. SCOPE = [ "user-read-email", "playlist-read-collaborative", "playlist-modify-public" ] – Bench Vue Jan 31 '23 at 22:33
  • I always get "An exception occurred" Thank you so much for the help but I think im going to give up, took so long for nothing and I literraly copy paste everything that I did on my side and It didnt work... Thanks man ! – xif Feb 01 '23 at 22:58
  • OK, your account not allow `playlist-modify-public` scope. Thanks for your following. – Bench Vue Feb 01 '23 at 23:40
  • I don't see any mentions about adding the scope `playlist-modify-private`... – Ximzend Feb 04 '23 at 19:12
  • @Ximzend, The `playlist-modify-private` is required scope for changing playlist title. But we can check not easy way. Who questioner has not scope , mine has a scope. We don't know reason. But I can test it scope exist or not in other question (call [Flask](https://stackoverflow.com/questions/75286588/spotify-web-api-call-gives-wrong-code-python/75292843#75292843) example). It can get the an access token with test scope. – Bench Vue Feb 04 '23 at 21:36
  • I checked it with [the Get Playlist endpoint](https://developer.spotify.com/console/get-playlist/?playlist_id=6rkyVmrfPEeWkJm01mbhO1&market=&fields=public&additional_types=), and it says the playlist is not public (aka not on profile). So, the `playlist-modify-public` scope isn't sufficient, and the `playlist-modify-private` is required. – Ximzend Feb 05 '23 at 07:46
  • Thanks, how to see `playlist-modify-private` scope? – Bench Vue Feb 05 '23 at 12:31
  • All scopes [are listed here](https://developer.spotify.com/documentation/general/guides/authorization/scopes/) – Ximzend Feb 05 '23 at 20:13
  • @Ximzend, I tested with `playlist-modify-public` scope. It works to change the title and description of my playlist. – Bench Vue Feb 10 '23 at 18:49
  • Because **your** playlist is public, but **their** playlist **isn't** public. – Ximzend Feb 10 '23 at 19:05