Используя приведенный ниже код, я могу получить объект playlists для своего пользователя, но список для записи items пуст. У меня есть пара сотен плейлистов, поэтому я должен что-то упустить в этом коде.
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
И вывод:
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})






Я думаю, что library и playlist — это разные ресурсы, к которым у вас есть доступ.
Вместо этого вам может потребоваться применить область playlist-read-private.
playlist-read-private
- Description: Read access to user's private playlists.
- Visible to users: Access your private playlists.
Endpoints that require the
playlist-read-privatescope
- Check if Users Follow a Playlist
- Get a List of Current User's Playlists
- Get a List of a User's Playlists
В то время как область user-library-read, похоже, не предоставляет доступ к вашим плейлистам.
Источник: https://developer.spotify.com/documentation/general/guides/scopes/#user-library-read
Playlist-read-private работает, но не с кодом в вопросе. Я поставил другой ответ для формата.
Кажется, это работает. Однако он не возвращает папки плейлистов. И я еще не нашел ни одного упоминания о таком.
#!/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)
Это не имело значения. У меня есть результаты с использованием
playlists = sp.current_user_playlists(), все остальное то же самое.