我正在尝试使用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。
发布于 2018-04-07 21:59:47
发布于 2020-05-27 16:26:34
所以我也一直在尝试访问我现在的歌曲,在网络上跟踪每一个错误的答案3天后……我正在使用Authorization Code Flow,正如这里所说的,https://spotipy.readthedocs.io/en/2.12.0/#client-credentials-flow。
只有不访问用户信息的端点才能被访问。
由于无法设置环境变量,所以我使用了另一个脚本。
注意,您应该添加一个重定向链接,如"https://localhost:8000/" in https://developer.spotify.com/dashboard/applications/。
下面是我添加信用要素的代码:
import os
class SetCreditentials:
def __init__(self):
self.CLIENT_SECRET = os.getenv('SPOTIPY_CLIENT_SECRET')
self.CLIENT_ID = os.getenv('SPOTIPY_CLIENT_ID')
self.REDIRECT_URI = os.getenv('SPOTIPY_REDIRECT_URI')
os.environ['SPOTIPY_CLIENT_ID'] = ""
os.environ['SPOTIPY_CLIENT_SECRET'] = ""
os.environ['SPOTIPY_REDIRECT_URI'] = "http://localhost:8000/"以下是访问任何数据的代码(此处的当前播放轨道):
import spotipy.util as util
import spotipy
import Creditentials
# Authorization Code Flow
user = "213tzif5o7rzyxtijuqdgtfuq"
scope = "user-read-currently-playing"
Creditentials.SetCreditentials()
token = util.prompt_for_user_token(user, scope)
track = spotipy.Spotify(token).current_user_playing_track()
print(track)https://stackoverflow.com/questions/49712100
复制相似问题