Reputation: 17
I'm trying to access music that the user is currently playing using the spotipy Spotify python library.
import json
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
cid = "xxx"
csecret = "xxx"
redirectURI = "xxx"
username = "xxx"
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=csecret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
scope = 'user-read-currently-playing'
token = util.prompt_for_user_token(username, scope, cid, csecret, redirectURI)
if token:
sp = spotipy.Spotify(auth=token)
else:
print("Can't get token for", username)
current_track = sp.current_user_playing_track()
print(json.dumps(current_track, sort_keys=False, indent=4))
I've also tried using sp.currently_playing()
. I am able to access other data such as sp.current_user_saved_tracks(limit=3, offset=0)
. Using what I currently have, it always errors out saying
AttributeError: 'Spotify' object has no attribute 'current_user_playing_track'
. I've explored using node instead but I really would like to stick with python.
Upvotes: 1
Views: 1618
Reputation: 29
So I have been trying to acces my current song too, after 3 days following every wrong answer on the web...
I am using the Authorization Code Flow
as said here https://spotipy.readthedocs.io/en/2.12.0/#client-credentials-flow.
Only endpoints that do not access user information can be accessed
As I couldn't set the environement variables, I did it with another script.
Note that you should add a redirection link such as "https://localhost:8000/" in https://developer.spotify.com/dashboard/applications/.
Here is my code to add my creditentials:
import os
class SetCreditentials:
def __init__(self):
self.CLIENT_SECRET = os.getenv('SPOTIPY_CLIENT_SECRET')
self.CLIENT_ID = os.getenv('SPOTIPY_CLIENT_ID')
self.REDIRECT_URI = os.getenv('SPOTIPY_REDIRECT_URI')
os.environ['SPOTIPY_CLIENT_ID'] = ""
os.environ['SPOTIPY_CLIENT_SECRET'] = ""
os.environ['SPOTIPY_REDIRECT_URI'] = "http://localhost:8000/"
Here is the code to access any data (the current playing track here):
import spotipy.util as util
import spotipy
import Creditentials
# Authorization Code Flow
user = "213tzif5o7rzyxtijuqdgtfuq"
scope = "user-read-currently-playing"
Creditentials.SetCreditentials()
token = util.prompt_for_user_token(user, scope)
track = spotipy.Spotify(token).current_user_playing_track()
print(track)
Upvotes: 1
Reputation: 365617
You're trying to use a feature that isn't in the latest released version yet.
You can see on PyPI that the current version was released on 5 Jan 2017, but you can see on Github that the function you want to call was added on 13 May 2017.
Issue #270 and Issue #211 are both asking when a new release will be pushed to PyPI, (Also, there's some weird issue with the version numbering.)
Anyway, as #211 says:
If you're running into issues still, you can install the package directly from the github repo.
pip install git+https://github.com/plamere/spotipy.git --upgrade
Upvotes: 2