如何解决:使用Spotipy在Python中的客户端范围不足

时间:2019-05-16 16:38:22

标签: python authentication oauth spotify spotipy

当我尝试在Spotipy中运行cretin模块时,收到一条客户端作用域错误消息。 Spotify出现403客户端错误,提示“未提供令牌”

仅尝试了几个模块(例如current_user()模块)来尝试多个模块,而其他模块则吐出了客户端作用域错误错误。

仅尝试了几个模块(例如current_user()模块)来尝试多个模块,而其他模块则吐出了客户端作用域错误错误。

import spotipy
import os
import json
import sys
import webbrowser
import spotipy.util as util
from json.decoder import JSONDecodeError


username = sys.argv[0]

try:
    token = util.prompt_for_user_token(username)

except:
    os.remove(f".cache-{username}")
    token = util.prompt_for_user_token(username)


spotify = spotipy.Spotify(auth=token)

albums = spotify.current_user_saved_albums(limit= 100, offset = 0)
userplaylist = spotify.current_user_playlists()
spuser= spotify.current_user()


class spotifyUser:

    def __init__(self, userid, followers, playlist):
        self.userid = userid
        self.followers = followers
        self.playlist = playlist
        self.albums


#list all user playlists
def all_playlists(playlist):

    for r in range(len(playlist['items'])):
        print(playlist['items'][r]['name'])

currentuser = spotifyUser(spuser['display_name'],spuser['followers'] 
['total'], userplaylist)

#all_playlists(currentuser.playlist)
print(albums)

运行时间会导致:

Traceback (most recent call last):
  File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\spotipy\client.py", line 119, in _internal_call
    r.raise_for_status()
  File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\requests\models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.spotify.com/v1/me/albums?limit=100&offset=0

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:/Python37/codes/Testspotipy.py", line 20, in <module>
        albums = spotify.current_user_saved_albums(limit= 100, offset = 0)
      File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\spotipy\client.py", line 585, in current_user_saved_albums
        return self._get('me/albums', limit=limit, offset=offset)
      File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\spotipy\client.py", line 146, in _get
        return self._internal_call('GET', url, payload, kwargs)
      File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\spotipy\client.py", line 124, in _internal_call
        headers=r.headers)
    spotipy.client.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/me/albums?limit=100&offset=0:
     Insufficient client scope

1 个答案:

答案 0 :(得分:1)

在提示进行令牌调用的提示中传递一个额外的arg,作用域为以空格分隔的字符串:

token = util.prompt_for_user_token(username, scope="user-library-read <etc>")

List of available Scopes