简体   繁体   English

Spotipy用户名无效?

[英]Spotipy invalid username?

I have a twitch bot for a streamer I know and I'm trying to make a command that shows the song he is currently listening to on spotify. 我有一个认识的流媒体播放器,我正试图发出一个命令来显示他当前正在Spotify上收听的歌曲。

I found the Spotipy library that does this, but I'm getting an invalid username error with the following code: 我找到了执行此操作的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)

In my code I filled in the credentials ofcourse. 在我的代码中,我填写了课程证书。 So this code returns me this error: 因此,这段代码向我返回了此错误:

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]

I'm not exactly sure why this is going wrong. 我不确定为什么会出错。 Can anyone point me in the right direction? 谁能指出我正确的方向?

Additionally/alternatively: How could I use the requests library to do bearer authentication? 另外/或者:如何使用请求库进行承载身份验证? (I tried to do the request manually in postman and filled in the client ID, and it gave me this error: "message": "Only valid bearer authentication supported") (我尝试在邮递员中手动执行请求,并填写了客户端ID,这给了我这个错误:“消息”:“仅支持有效的承载身份验证”)

Don't use this form of authorization. 不要使用这种形式的授权。 The reason you run into this is because you're making anonymous API calls. 遇到此问题的原因是因为您正在进行匿名API调用。 This method requires the oAuth authorization to make these calls. 此方法需要oAuth授权才能进行这些调用。 Set the username and appropriate scope: 设置用户名和适当的范围:

username = "myUsername"
scope = "user-read-currently-playing"

You'll need your application's redirect URI: 您将需要应用程序的重定向URI:

redirect_uri = "http://localhost:8888/callback/"

Set token to this: 为此设置令牌:

token = util.prompt_for_user_token(username, scope, CLIENT_ID, CLIENT_SECRET, redirect_uri)

Now you can instantiate your Spotify object. 现在,您可以实例化Spotify对象。

sp = spotipy.Spotify(auth=token)

You should be good from there. 从那里你应该很好。 A window will pop-up prompting you to authenticate yourself, but afterwards, it will no longer be necessary because it will be in cache. 将会弹出一个窗口,提示您进行身份验证,但是此后,将不再需要它,因为它将位于缓存中。

In case you get it to work, you can extract data like this: 万一您可以使用它,可以提取如下数据:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM