Spotipy: util.prompt_for_user_token() and GUI

Created on 16 Jun 2020  路  4Comments  路  Source: plamere/spotipy

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?

Most helpful comment

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.

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DylanMeeus picture DylanMeeus  路  4Comments

sheheryarbutt picture sheheryarbutt  路  5Comments

StefanRickli picture StefanRickli  路  3Comments

gauthamanms picture gauthamanms  路  3Comments

tatoosh picture tatoosh  路  5Comments