【问题标题】:Spotipy invalid username?Spotipy 无效的用户名?
【发布时间】:2017-12-03 08:07:02
【问题描述】:

我有一个用于我认识的流媒体的 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)

在我的代码中,我填写了课程凭据。所以这段代码返回了这个错误:

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]

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

另外/替代地: 如何使用 requests 库进行承载身份验证? (我尝试在邮递员中手动执行请求并填写客户端ID,它给了我这个错误:“消息”:“仅支持有效的承载身份验证”)

【问题讨论】:

    标签: python spotify spotipy


    【解决方案1】:

    不要使用这种形式的授权。您遇到此问题的原因是您正在进行匿名 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多