mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 00:25:54 +03:00
[GH-ISSUE #781] get_cached_token() when multiple users are connected #482
Labels
No labels
api-bug
bug
dependencies
documentation
duplicate
enhancement
external-ide
headless-mode
implicit-grant-flow
invalid
missing-endpoint
pr-welcome
private-api
pull-request
question
spotipy3
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotipy#482
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @NicoCaldo on GitHub (Feb 13, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/781
I'm building an app where users can log in with Spotify to do stuff.
Now, I have set up the login flow but I have an issue with
get_cached_token()function. It seems that it saves only one toke. Is it the correct behavior?I'm asking because my app has multiple users, everyone needs to access Spotify so a token is created for each one of the users.
Do I need to extend or create my own
get_cached_token()function to manage the link between tokens and users?@Peter-Schorn commented on GitHub (Feb 14, 2022):
Which one? There are multiple classes in this repo with this method.
Are you building a web app? If so, are you saying that you want multiple users to log in within the same browser session? Or are you simply saying that multiple clients will connect your web app?
@NicoCaldo commented on GitHub (Feb 14, 2022):
@Peter-Schorn thanks for your answer.
I'm building a web app where users can register and log in to it (so multiple access connected to my web app). once a user is registered, it can connect its Spotify account. On the backend I handle this connection with
spotify_oauth2().get_authorize_url()and then I request the access token withacc_token = spotify_oauth2().get_access_token(spotify_code)where spotify_code is the one I got on the URL from the callbackI got a function that check if the user is already logged using
sp_auth = spotify_oauth2()andtoken_info = sp_auth.get_cached_token()but, in this way in the cache, there's always only one access token from the last user that requests something.I'm wondering if there's a way to have the access token split per user or do I need to build it myself?
@Peter-Schorn commented on GitHub (Feb 14, 2022):
See this example. You should be using a
CacheFileHandlerwith a cache path that is unique to the browser session so that each session stores a separate copy of the authorization info. Otherwise, each successive user will overwrite the authorization info for the last user.@NicoCaldo commented on GitHub (Feb 14, 2022):
Thanks a lot for the explanation. So, in this way, I can also use the
refresh_access_token(refresh_token)method to refresh it, can I?@Peter-Schorn commented on GitHub (Feb 14, 2022):
Do not call any methods that refresh the access token directly. This will be done automatically for you.
Also, for future reference, when you mention a method, you need to also mention the class that it belongs to. There are multiple classes with a
refresh_access_tokenmethod in this repo.@NicoCaldo commented on GitHub (Feb 14, 2022):
Thanks for the tips. I'm knew to this library so I'm learning along the way :)
@NicoCaldo commented on GitHub (Feb 14, 2022):
@Peter-Schorn
So I have tried to use the cache handler as you suggest with the following code
every user logged in has a unique username so the code correctly creates different cache files with different keys.
Now, I have tried login in with a user, linking the Spotify account, and getting the playlists of the account. Everything goes smoothly. The issue appears when I log out from the account and log in to another account and try to link to another Spotify Account. If you first don't log out from Spotify via browser, it will load the account that has been already logged but, it still creates a different cache file
@Peter-Schorn commented on GitHub (Feb 15, 2022):
Pass in
Truefor theshow_dialogparameter of the initializer forSpotifyOAuth.@stephanebruckert commented on GitHub (Jul 9, 2024):
Seems resolved, closing