我正在尝试使用 spotipy Spotify python 库访问用户当前正在播放的音乐。
import json
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
cid = "xxx"
csecret = "xxx"
redirectURI = "xxx"
username = "xxx"
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=csecret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
scope = 'user-read-currently-playing'
token = util.prompt_for_user_token(username, scope, cid, csecret, redirectURI)
if token:
sp = spotipy.Spotify(auth=token)
else:
print("Can't get token for", username)
current_track = sp.current_user_playing_track()
print(json.dumps(current_track, sort_keys=False, indent=4))
我也尝试过使用sp.currently_playing()
. 我能够访问其他数据,例如sp.current_user_saved_tracks(limit=3, offset=0)
. 使用我目前拥有的东西,它总是会出错
AttributeError: 'Spotify' object has no attribute 'current_user_playing_track'
。我已经探索过使用节点,但我真的很想坚持使用 python。