【问题标题】:python spotipy playlists[items] is emptypython spotipy 播放列表 [项目] 为空
【发布时间】:2019-01-25 03:01:36
【问题描述】:

使用下面的代码,我可以为我的用户检索 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})

【问题讨论】:

    标签: python spotipy


    【解决方案1】:

    我认为libraryplaylist 是您可以访问的不同资源。

    您可能需要改为应用 playlist-read-private 范围。

    播放列表读取私有

    • 说明:拥有对用户私人播放列表的读取权限。
    • 对用户可见:访问您的私人播放列表。

    需要playlist-read-private 范围的端点

    • 检查用户是否关注播放列表
    • 获取当前用户的播放列表列表
    • 获取用户播放列表的列表

    虽然user-library-read 范围似乎无法访问您的播放列表。

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

    【讨论】:

    • 这没什么区别。我使用playlists = sp.current_user_playlists() 得到的结果都一样。
    • 播放列表读取私有有效,但不适用于问题中的代码。我为格式提出了其他答案。
    【解决方案2】:

    这似乎有效。但它不会返回播放列表文件夹。而且我还没有发现任何关于此类的可疑提及。

     #!/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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      相关资源
      最近更新 更多