Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Spotipy无效用户名?_Python_Spotify_Spotipy - Fatal编程技术网

Python Spotipy无效用户名?

Python Spotipy无效用户名?,python,spotify,spotipy,Python,Spotify,Spotipy,我有一个用于拖缆的twitch机器人,我知道,我正试图发出一个命令,显示他目前正在spotify上收听的歌曲 我找到了执行此操作的Spotipy库,但出现了一个无效的用户名错误,错误代码如下: import spotipy import spotipy.util as util CLIENT_ID = 'xx' CLIENT_SECRET = 'xx' token = util.oauth2.SpotifyClientCredentials(client_id=CLIENT_ID, clie

我有一个用于拖缆的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]
回溯(最近一次呼叫最后一次):
文件“/Users/Pascalschilp/Documents/spot/spotipy master/lol.py”,第13行,在
currentsong=sp.current\u正在播放('spotify:user:passle')
文件“/Users/Pascalschilp/Documents/spot/spotipy master/spotipy/client.py”,第899行,当前正在播放
返回自我。_get(“我/玩家/正在玩”,市场=市场)
文件“/Users/Pascalschilp/Documents/spot/spotipy master/spotipy/client.py”,第148行,在
返回self.\u内部调用('GET',url,payload,kwargs)
文件“/Users/Pascalschilp/Documents/spot/spotipy master/spotipy/client.py”,第126行,内部调用
headers=r.headers)
spotipy.client.SpotifyException:http状态:404,代码:-1-https://api.spotify.com/v1/me/player/currently-playing?market=spotify%3Auser%3Apassle:
无效用户名
[在1.2秒内完成,退出代码为1]
[shell_cmd:python-u”/Users/Pascalschilp/Documents/spot/spotipy master/lol.py“]
[dir:/Users/Pascalschilp/Documents/spot/spotipy master]
[路径:/usr/bin:/bin:/usr/sbin:/sbin]
我不太清楚为什么会出问题。谁能给我指出正确的方向吗

另外/或者:
如何使用请求库进行承载身份验证?(我试图在postman中手动执行请求并填写客户ID,但它给了我以下错误:“消息”:“仅支持有效的承载身份验证”)

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