简体   繁体   中英

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.

I found the Spotipy library that does this, but I'm getting an invalid username error with the following code:

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")

Don't use this form of authorization. The reason you run into this is because you're making anonymous API calls. This method requires the oAuth authorization to make these calls. Set the username and appropriate scope:

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

You'll need your application's redirect 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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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