首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spotipy无效用户名?

Spotipy无效用户名?
EN

Stack Overflow用户
提问于 2017-10-22 21:41:06
回答 1查看 3.9K关注 0票数 0

我有一个抽搐机器人的一个流线,我知道,我正在尝试做一个命令,显示他目前正在听的歌曲在spotify。

我找到了这样做的Spotipy库,但是使用以下代码我得到了一个无效的用户名错误:

代码语言:javascript
复制
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)

当然,在我的代码中,我填写了凭证。因此,这段代码返回了以下错误:

代码语言:javascript
复制
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":“只支持有效的承载身份验证”)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-24 15:45:21

不要使用这种授权形式。您遇到这种情况的原因是您正在进行匿名API调用。此方法需要oAuth授权才能进行这些调用。设置用户名和适当的作用域:

代码语言:javascript
复制
username = "myUsername"
scope = "user-read-currently-playing"

您需要应用程序的重定向URI:

代码语言:javascript
复制
redirect_uri = "http://localhost:8888/callback/"

将令牌设置为:

代码语言:javascript
复制
token = util.prompt_for_user_token(username, scope, CLIENT_ID, CLIENT_SECRET, redirect_uri)

现在您可以实例化Spotify对象了。

代码语言:javascript
复制
sp = spotipy.Spotify(auth=token)

你应该做得很好。一个窗口将弹出,提示您验证自己,但之后,它将不再必要,因为它将在缓存中。

为了让它正常工作,您可以提取这样的数据:

代码语言:javascript
复制
song_name = currentsong['item']['name']
song_artist = currentsong['item']['artists'][0]['name']
print("Now playing {} by {}".format(song_name, song_artist))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46879418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档