Spotipy 无效的用户名?

Spotipy invalid username?

我有一个我认识的主播的 twitch 机器人,我正在尝试发出一个命令来显示他当前正在 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)

在我的代码中,我填写了当然的凭据。所以这个代码 returns 我这个错误:

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]

我不确定为什么会出错。谁能指出我正确的方向?

Additionally/alternatively: 我如何使用请求库进行不记名身份验证? (我尝试在邮递员中手动执行请求并填写客户端ID,它给了我这个错误:"message":"Only valid bearer authentication supported")

不要使用这种形式的授权。您 运行 这样做的原因是因为您正在进行匿名 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)

你应该从那里开始。将弹出 window 提示您进行身份验证,但之后将不再需要,因为它将在缓存中。

如果你让它工作,你可以像这样提取数据:

song_name = currentsong['item']['name']
song_artist = currentsong['item']['artists'][0]['name']
print("Now playing {} by {}".format(song_name, song_artist))