[GH-ISSUE #733] Refresh user token periodically #442

Closed
opened 2026-02-27 23:22:39 +03:00 by kerem · 3 comments
Owner

Originally created by @fluffy-cakes on GitHub (Oct 7, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/733

Hi, I have searched the FAQ and Issues, but still can't find, or seem to understand, how to implement an automatic token refresh without having to close and re-open the app/script. My current design is a Spotify voting web app that runs in Flask, allowing users to vote up/down the song to keep/skip playing it.

I currently use this as authorisation, as I believe that this method is required to poll the users 'currently playing' tracks.

username = 'asdfqwer'
scope    = 'user-read-currently-playing,user-modify-playback-state'

token    = spotipy.util.prompt_for_user_token(username, scope)
sp       = spotipy.Spotify(auth=token)

Later on I use sp.current_user_playing_track() to get the track and to grab some values from the output. This happens every second, updating the web app with the current track details. Within the hour, obviously, the cached token expires, and I will need to stop my running app and start it again. Not currently ideal, as I'm going to be using this over the space of 2-3 hours in a session.

I did think about running a background task to spin of a subprocess just to log in, hopefully updating the token as it does. But this doesn't seem to work either:

def spotifyBg():
    def subThread():
        while True:
            print("Running subprocess")
            subprocess.Popen(['/root/partify/flask/04/spotify_bg.py'])
            sleep(240)

    thread = threading.Thread(target=subThread)
    thread.start()

I am not new to Python, but I am also not anything great. Can anyone please direct me on how I can achieve the above?

Originally created by @fluffy-cakes on GitHub (Oct 7, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/733 Hi, I have searched the FAQ and Issues, but still can't find, or seem to understand, how to implement an automatic token refresh **without** having to close and re-open the app/script. My current design is a Spotify voting web app that runs in Flask, allowing users to vote up/down the song to keep/skip playing it. I currently use this as authorisation, as I believe that this method is required to poll the users 'currently playing' tracks. ```python username = 'asdfqwer' scope = 'user-read-currently-playing,user-modify-playback-state' token = spotipy.util.prompt_for_user_token(username, scope) sp = spotipy.Spotify(auth=token) ``` Later on I use `sp.current_user_playing_track()` to get the track and to grab some values from the output. This happens every second, updating the web app with the current track details. Within the hour, obviously, the cached token expires, and I will need to stop my running app and start it again. Not currently ideal, as I'm going to be using this over the space of 2-3 hours in a session. I did think about running a background task to spin of a subprocess just to log in, hopefully updating the token as it does. But this doesn't seem to work either: ```python def spotifyBg(): def subThread(): while True: print("Running subprocess") subprocess.Popen(['/root/partify/flask/04/spotify_bg.py']) sleep(240) thread = threading.Thread(target=subThread) thread.start() ``` I am not new to Python, but I am also not anything great. Can anyone please direct me on how I can achieve the above?
kerem 2026-02-27 23:22:39 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@stephanebruckert commented on GitHub (Oct 7, 2021):

prompt_for_user_token is deprecated, please avoid using it as it was only there to quickly help running an app and I'm not sure it refreshes the token for you. The new way to do it is displayed in the README.

Also the FAQ specifically mentions building an API with Flask https://github.com/plamere/spotipy/blob/master/FAQ.md#how-to-use-spotipy-in-an-api, in which the token refresh is handled automatically. Did you have a look at it? Let us know what blocks there.

<!-- gh-comment-id:937725239 --> @stephanebruckert commented on GitHub (Oct 7, 2021): `prompt_for_user_token` is deprecated, please avoid using it as it was only there to quickly help running an app and I'm not sure it refreshes the token for you. The new way to do it is displayed in the README. Also the FAQ specifically mentions building an API with Flask https://github.com/plamere/spotipy/blob/master/FAQ.md#how-to-use-spotipy-in-an-api, in which the token refresh is handled automatically. Did you have a look at it? Let us know what blocks there.
Author
Owner

@fluffy-cakes commented on GitHub (Oct 7, 2021):

Cheers for the prompt reply, I am looking into that flask example and then the auth handling. I'll reply back or close once I understand them.

<!-- gh-comment-id:937831527 --> @fluffy-cakes commented on GitHub (Oct 7, 2021): Cheers for the prompt reply, I am looking into that flask example and then the auth handling. I'll reply back or close once I understand them.
Author
Owner

@fluffy-cakes commented on GitHub (Oct 7, 2021):

@stephanebruckert ; cheers for that. from those examples i was able to get working what i wanted 👍🏼

<!-- gh-comment-id:937905091 --> @fluffy-cakes commented on GitHub (Oct 7, 2021): @stephanebruckert ; cheers for that. from those examples i was able to get working what i wanted 👍🏼
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#442
No description provided.