如何解决:使用 Spotipy 的 Python 中客户端范围不足
Posted
技术标签:
【中文标题】如何解决:使用 Spotipy 的 Python 中客户端范围不足【英文标题】:How to Solve: Insufficient client scope in Python using Spotipy 【发布时间】:2019-10-03 23:57:24 【问题描述】:当我尝试在 Spotipy 中运行 cretin 模块时,我收到一条 Insufficient client scope 错误消息。 Spotify 给出 403 客户端错误,提示“未提供令牌”
仅尝试了多个模块,例如 current_user() 模块,但其他模块返回 Insufficient client scope 错误。
仅尝试了多个模块,例如 current_user() 模块,但其他模块返回 Insufficient client scope 错误。
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
【问题讨论】:
您在进行身份验证时需要 user-library-read 范围,而不是在 python 示例中显示它是如何完成的,但您需要提供它,错误消息可能是指这个缺失 【参考方案1】:在令牌调用提示符中传递一个额外的 arg,范围为以空格分隔的字符串:
token = util.prompt_for_user_token(username, scope="user-library-read <etc>")
List of available Scopes
【讨论】:
尝试并得到了异常 Traceback(最近一次调用最后):文件“C:/Python37/codes/Testspotipy.py”,第 13 行,在以上是关于如何解决:使用 Spotipy 的 Python 中客户端范围不足的主要内容,如果未能解决你的问题,请参考以下文章
获得使用 Python3 的 spotipy 修改 Spotify 播放列表的授权