Hello,
I'm writing an app controlling spotify playback, which has GUI. Unfortunately util.prompt_for_user_token() is using console to enter URL. I've also tried using automatic authorization process, but for some reason it isn't working.
Is there some other way to authorize user?
You can use a SpotifyOAuth object.
import webbrowser
from spotipy import SpotifyOAuth
auth = SpotifyOAuth(client_id, client_secret, redirect_uri, scope=scopes)
# If the parameters are set as ENV variables, you can omit them
auth_url = auth.get_authorize_url
try:
webbrowser.open(auth_url)
logger.info("Opened %s in your browser", auth_url)
except webbrowser.Error:
logger.error("Please navigate here: %s", auth_url)
# GUI Input to get response_url
code = auth.parse_response_code(response_url)
auth_token = auth.get_access_token(code, as_dict=False)
Also, if you are making your app available to untrusted clients, be wary about exposing your client secret.
Thank you!
Just two minor fixes:
from spotipy import SpotifyOAuth
should be:
from spotipy.oauth2 import SpotifyOAuth
and
auth_token = auth.get_access_token(code, as_dict=False)
was throwing error so I used:
token = auth.get_access_token(code)['access_token']
What was the error on the second one? as_dict=True (the default for get_access_token) is deprecated, so it is not reliable.
This looks solved
Most helpful comment
You can use a
SpotifyOAuthobject.Also, if you are making your app available to untrusted clients, be wary about exposing your client secret.