Stephen Boston
Stephen Boston

Reputation: 1189

python spotipy playlists[items] is empty

Using the code below I can retrieve a playlists object for my user but the list for the items entry is empty. I have a couple hundred playlists so I must be missing something in this code.

 import spotipy
 import spotipy.util as util

 username='xxxxx'
 clientid='xxxxx'
 clientsecret='xxxxx'
 redirecturi='http://localhost'
 thescope='user-library-read'

 print("Requesting token...")
 token = util.prompt_for_user_token(username,scope=thescope,client_id=clientid,client_secret=clientsecret,redirect_uri=redirecturi)
 print("Token is %s" % token)
 if token:
     sp = spotipy.Spotify(auth=token)
     playlists = sp.user_playlists(username)
     print("Playlists are ", playlists)
 else:
     print "Can't get token for", username

And output is :

 Requesting token...
 Token is<token>
 ('Playlists are ', {u'items': [], u'next': None, u'href': u'https://api.spotify.com/v1/users/havanon/playlists?offset=0&limit=50', u'limit': 50, u'offset': 0, u'total': 0, u'previous': None})

Upvotes: 0

Views: 1027

Answers (2)

Stephen Boston
Stephen Boston

Reputation: 1189

This seems to work. It does not return playlist folders though. And I haven't yet found any spotipy mention of such.

 #!/usr/bin/env python2
 import sys
 import spotipy
 import spotipy.util as util

 username='xxx'
 clientid='xxx'
 clientsecret='xxx'
 redirecturi='http://localhost'
 thescope='playlist-read-private'

 token = util.prompt_for_user_token(username,scope=thescope,client_id=clientid,client_secret=clientsecret,redirect_uri=redirecturi)
 if token:
     sp = spotipy.Spotify(auth=token)
     playlists = sp.current_user_playlists()
     while playlists:
         for i, playlist in enumerate(playlists['items']):
             print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'],  playlist['name']))
         if playlists['next']:
             print("getting next 50")
             playlists = sp.next(playlists)
         else:
             playlists = None
 else:
     print ("Can't get token for", username)

Upvotes: 0

s3bw
s3bw

Reputation: 3049

I think the library and the playlist are different resources that you can access.

You might need to apply the playlist-read-private scope instead.

playlist-read-private

  • Description: Read access to user's private playlists.
  • Visible to users: Access your private playlists.

Endpoints that require the playlist-read-private scope

  • Check if Users Follow a Playlist
  • Get a List of Current User's Playlists
  • Get a List of a User's Playlists

While the user-library-read scope doesn't seem to provide access to your playlists.

Source: https://developer.spotify.com/documentation/general/guides/scopes/#user-library-read

Upvotes: 2

Related Questions