如何解决:Python 中使用 Spotipy 的客户端范围不足
How to Solve: Insufficient client scope in Python using Spotipy
当我尝试 运行 Spotipy 中的 cretin 模块时,我收到了客户端范围不足的错误消息。 Spotify 给出了一个 403 客户端错误说 "No Token Provided"
尝试了多个模块只有少数工作,例如 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
将额外的 arg 传递到令牌调用提示中,范围为以空格分隔的字符串:
token = util.prompt_for_user_token(username, scope="user-library-read <etc>")
当我尝试 运行 Spotipy 中的 cretin 模块时,我收到了客户端范围不足的错误消息。 Spotify 给出了一个 403 客户端错误说 "No Token Provided"
尝试了多个模块只有少数工作,例如 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
将额外的 arg 传递到令牌调用提示中,范围为以空格分隔的字符串:
token = util.prompt_for_user_token(username, scope="user-library-read <etc>")