我正在编写一个脚本,该脚本从Spotify返回您当前正在播放的歌曲。
我阅读了Spotify API的文档,并且一切正常,但是尝试实现某些模块时遇到了一些问题。
例如,这里https://spotipy.readthedocs.io/en/latest/说有一个名为currently_playing()
的模块,但出现此错误:
Traceback (most recent call last):
File "spotify_test.py", line 21, in <module>
current_song = sp.currently_playing()
AttributeError: 'Spotify' object has no attribute 'currently_playing'
到目前为止,这是我的代码,当我更改范围和模块以获取用户的播放列表时,它可以很好地工作。因此令牌不是问题。
import sys
import spotipy
import spotipy.util as util
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print('Usage %s Username ' % (sys.argv[0],))
sys.exit()
scope = 'user-read-currently-playing'
token = spotipy.util.prompt_for_user_token(
username, scope, redirect_uri='http://127.0.0.1/callback')
if token:
sp = spotipy.Spotify(auth=token)
current_song = sp.currently_playing()
else:
print("Can't get token for", username)
print(current_song)