Spotipy.User_playlist引发错误:错误请求,其中有多个请求

时间:2019-05-15 22:18:00

标签: python spotify spotipy

我正在尝试使用Spotipy获得我的两个播放列表。我正在使用spotipy.user_playlist()方法。目前,它仅适用于一个播放列表,但是每当尝试第二个播放列表时,都会出现错误。两个Spotify URI均有效,正如我之前提到的,我得到的第一个播放列表非常好。这是播放列表URI:

spotify:user:22tu7wr2adbylfh3n7jns7oga:playlist:7G8WJOoHzdwj81qzRDwAnO
spotify:user:22tu7wr2adbylfh3n7jns7oga:playlist:4yxnkgxG3CHayD4wACjb2y

我尝试使用2个不同的授权应用创建第二个授权调用,使用了来自不同用户的不同播放列表,还尝试了所有我找不到的授权方法。

import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.oauth2 as oauth2
cid ="****" 
secret = "****"
username = "2tu7wr2adbylfh3n7jns7oga"
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret) 
scope = 'user-library-read playlist-read-private'
token = util.prompt_for_user_token(username, scope, client_id=cid, client_secret=secret, redirect_uri='http://localhost/callback')
sp = spotipy.Spotify(auth=token, client_credentials_manager=client_credentials_manager)
if token:
    good = "4yxnkgxG3CHayD4wACjb2y"
    bad = "7G8WJOoHzdwj81qzRDwAnO"
    good_playlist = sp.user_playlist(username, playlist_id=good)
    bad_playlist = sp.user_playlist(username, playlist_id=bad)
else:
    print("Can't get token for", username)

这会产生错误:

HTTPError                                 Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/spotipy/client.py in _internal_call(self, method, url, payload, params)
    118         try:
--> 119             r.raise_for_status()
    120         except:

~/anaconda3/lib/python3.7/site-packages/requests/models.py in raise_for_status(self)
    939         if http_error_msg:
--> 940             raise HTTPError(http_error_msg, response=self)
    941 

HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/users/2tu7wr2adbylfh3n7jns7oga/playlists/7G8WJOoHzdwj81qzRDwAnO

During handling of the above exception, another exception occurred:

SpotifyException                          Traceback (most recent call last)
<ipython-input-7-b0cbbe8e4d4f> in <module>
     10     bad = "7G8WJOoHzdwj81qzRDwAnO"
     11     good_playlist = sp.user_playlist(username, playlist_id=good)
---> 12     bad_playlist = sp.user_playlist(username, playlist_id=bad)
     13 else:
     14     print("Can't get token for", username)

~/anaconda3/lib/python3.7/site-packages/spotipy/client.py in user_playlist(self, user, playlist_id, fields)
    376             return self._get("users/%s/starred" % (user), fields=fields)
    377         plid = self._get_id('playlist', playlist_id)
--> 378         return self._get("users/%s/playlists/%s" % (user, plid), fields=fields)
    379 
    380     def user_playlist_tracks(self, user, playlist_id=None, fields=None,

~/anaconda3/lib/python3.7/site-packages/spotipy/client.py in _get(self, url, args, payload, **kwargs)
    144         while retries > 0:
    145             try:
--> 146                 return self._internal_call('GET', url, payload, kwargs)
    147             except SpotifyException as e:
    148                 retries -= 1

~/anaconda3/lib/python3.7/site-packages/spotipy/client.py in _internal_call(self, method, url, payload, params)
    122                 raise SpotifyException(r.status_code,
    123                     -1, '%s:\n %s' % (r.url, r.json()['error']['message']),
--> 124                     headers=r.headers)
    125             else:
    126                 raise SpotifyException(r.status_code,

SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/2tu7wr2adbylfh3n7jns7oga/playlists/7G8WJOoHzdwj81qzRDwAnO:
 Bad request

当我转到链接时,它说:

{
  "error": {
    "status": 401,
    "message": "No token provided"
  }
}

但是,我有来自授权的令牌。我很困扰。如果这是重复的帖子,请告诉我,我在其他任何地方都找不到解决方案。

编辑:我改用了python sp.user_playlist_tracks()函数,现在它对两个播放列表都有效。

0 个答案:

没有答案