我有一个抽搐机器人的一个流线,我知道,我正在尝试做一个命令,显示他目前正在听的歌曲在spotify。
我找到了这样做的Spotipy库,但是使用以下代码我得到了一个无效的用户名错误:
import spotipy
import spotipy.util as util
CLIENT_ID = 'xx'
CLIENT_SECRET = 'xx'
token = util.oauth2.SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
cache_token = token.get_access_token()
sp = spotipy.Spotify(cache_token)
currentsong = sp.currently_playing()
print(currentsong)当然,在我的代码中,我填写了凭证。因此,这段代码返回了以下错误:
Traceback (most recent call last):
File "/Users/Pascalschilp/Documents/spot/spotipy-master/lol.py", line 13, in <module>
currentsong = sp.currently_playing('spotify:user:passle')
File "/Users/Pascalschilp/Documents/spot/spotipy-master/spotipy/client.py", line 899, in currently_playing
return self._get("me/player/currently-playing", market = market)
File "/Users/Pascalschilp/Documents/spot/spotipy-master/spotipy/client.py", line 148, in _get
return self._internal_call('GET', url, payload, kwargs)
File "/Users/Pascalschilp/Documents/spot/spotipy-master/spotipy/client.py", line 126, in _internal_call
headers=r.headers)
spotipy.client.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/currently-playing?market=spotify%3Auser%3Apassle:
Invalid username
[Finished in 1.2s with exit code 1]
[shell_cmd: python -u "/Users/Pascalschilp/Documents/spot/spotipy-master/lol.py"]
[dir: /Users/Pascalschilp/Documents/spot/spotipy-master]
[path: /usr/bin:/bin:/usr/sbin:/sbin]我不太清楚为什么会出错。谁能给我指明正确的方向?
另外/或者:如何使用请求库进行承载身份验证?(我尝试用postman手工完成请求,并填写客户ID,它给了我一个错误:"message":“只支持有效的承载身份验证”)
发布于 2017-10-24 15:45:21
不要使用这种授权形式。您遇到这种情况的原因是您正在进行匿名API调用。此方法需要oAuth授权才能进行这些调用。设置用户名和适当的作用域:
username = "myUsername"
scope = "user-read-currently-playing"您需要应用程序的重定向URI:
redirect_uri = "http://localhost:8888/callback/"将令牌设置为:
token = util.prompt_for_user_token(username, scope, CLIENT_ID, CLIENT_SECRET, redirect_uri)现在您可以实例化Spotify对象了。
sp = spotipy.Spotify(auth=token)你应该做得很好。一个窗口将弹出,提示您验证自己,但之后,它将不再必要,因为它将在缓存中。
为了让它正常工作,您可以提取这样的数据:
song_name = currentsong['item']['name']
song_artist = currentsong['item']['artists'][0]['name']
print("Now playing {} by {}".format(song_name, song_artist))https://stackoverflow.com/questions/46879418
复制相似问题