mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 00:25:54 +03:00
[GH-ISSUE #612] Can't get spotipy.Spotify() for multiple users #365
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#365
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 @alessiocelentano on GitHub (Nov 20, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/612
Hi! I asked this questions on StackOverflow but I didn't get an answer:
I'm creating a Telegram Bot to get Spotify stats with Spotipy. This script will be used by multiple people, but after a user give authorizations he gets my stats. I have no idea why this happens.
No problem with the code above, Bot redirects the user to the link (auth.get_authorize_url()) and get authorization successfully.
I think the problem is here, the variable returned is always referred to my Spotify account (and if I withdraw my authorization, it raises an error, although the user logged in with his account). This function has to return the right variable, can anyone help me? You can read the full code on this link
@Peter-Schorn commented on GitHub (Nov 21, 2020):
There are several problems with your code:
You are assigning two different authorization managers to
Spotify—SpotifyOAuthandSpotifyClientCredentials—which doesn't make sense. You only need one authorization manager. If you need to access user data, then you should useSpotifyOAuthorSpotifyPKCE. Don't ever use theoauth_managerorclient_credentials_managerparameters ofSpotify.init. @stephanebruckert we should deprecate these parameters and emit a warning when anyone tries to use them; they just create confusion.This method only performs the first step of the authorization process; you need to pass the authorization code that it returns into
SpotifyOAuth.get_access_tokento complete the authorization process. If you don't retain the authorization code thatget_auth_responsereturns, then calling it is completely pointless. The way that spotipy works is that it will automatically start the authorization process for you when you make a call to an endpoint, such asSpotify.me, if you are not authorized yet. It is this line that is authorizing your app, not the one above it.The user that the instance of
Spotifyreturned byget_clientis authorized for is which ever one logs in to their Spotify account in the browser, unless the authorization information is already stored in a cache file. Is this cache file the same for everyone that uses your app? If so then it probably contains your authorization information, and this is the reason that everyone is seeing the stats for your account.If, when you open the authorization URL, you are immediately redirected without being given a change to log in to your account, then it is because you are already logged in to your Spotify account on your browser. If you want to log in with a different account, then set
show_dialogtoTrueinSpotifyOAuth.init.What do you mean by this? Please be very specific about what you are doing in order to "withdraw my authorization".
Also, is everyone who uses your script supposed to get information about your Spotify account, instead of their Spotify account?
You need to provide more information about what you are trying to do. I'm very lost.
@Peter-Schorn commented on GitHub (Nov 21, 2020):
Also, you should only create one instance of
Spotifyfor each user. It looks like you're creating a new instance each timetop_artistsis called and then re-authorizing the user. That doesn't make any sense. You only need to authorize the user once.@alessiocelentano commented on GitHub (Nov 21, 2020):
Hi. Thank you for answering me and sorry for the inaccuracy of the question.
Okay, so the line should be
spotify = spotipy.client.Spotify(auth_manager=auth).So I can keep only a Spotify.me() and it start automatically the authorization process, right?
No, I pass an username in SpotifyOAuth(), so at the first authorization it creates
.cache-<username>. But if I have the username of the user who wants access to his data, how I get the right .cache? How doget_cached_token()work in this case, with multiple caches?Don't mind about that. I mean that if I delete the authorization on Spotify (Home > App > Remove access) and other users try to access, it gives an error, because it probably use my data stored in
.cache-<myusername>.No. They should get data about their account.
Simply this bot, once it gets authorization of the user and cached in
.cache-<username>, should give some user data (not of mine). There are many sites that do that, I wanted to implement that on a Telegram Bot.Perfect, my question is how I should get data of a specific .cache, instead of creating an instance every time. Is there a function for do that or I should do it manually?
@Peter-Schorn commented on GitHub (Nov 21, 2020):
If you provide the correct username when creating an instance of
SpotifyOAuth, thenSpotifyOAuth.get_cached_tokenshould only access the cache file containing that username. You should printstr(message.from_user.id)to the console to check that it is what you expect. You can also printSpotifyOAuth.usernameandSpotifyOAuth.cache_pathto see where the authorization manager is looking for the cached authorization information.You don't need to make a call to
Spotify.me()if your app is not actually using the data that it returns. Just remove it entirely and spotipy will go through the authorization process if necessary when you make a call tocurrent_user_top_tracksorcurrent_user_top_artistsor any of the other endpoints that your app ends up using.@alessiocelentano commented on GitHub (Nov 22, 2020):
Okay, one last thing: Once I get
token = SpotifyOAuth.get_cached.token()["access_token"]how should i do to get theSpotify()instance? I didspotipy.client.Spotify(auth=token)but it doesn't seem work.@stephanebruckert commented on GitHub (Nov 22, 2020):
You can get some inspiration from this example Flask app https://github.com/plamere/spotipy/blob/master/examples/app.py - it handles multiple users
When calling SpotifyOAuth you need to specify a cache_path, otherwise it will use the same cached token every time, therefore the one from the previous user:
github.com/plamere/spotipy@94e164385e/examples/app.py (L52)The name of the cache path needs to have a unique ID representing the user. In the case of the example it's a session ID. In the case of your bot, could it be a Telegram user ID?
github.com/plamere/spotipy@94e164385e/examples/app.py (L43)@alessiocelentano commented on GitHub (Nov 22, 2020):
I followed your advices and read the example, but it still doesn't work and I don't know why. You can take a look at the code here. I've been trying for days now and even with your help it doesn't seem to work, what am I doing wrong?
Brief explanation:
If the user is not logged, bot sends two buttons:
Spotify.me(), so access token is cachedThen the user can get his data, here there aren't problems. But an instance of my account is still created
@Peter-Schorn commented on GitHub (Nov 22, 2020):
Did you add the print statements I suggested? What did you find? It would be extremely helpful if you actually did that.
@alessiocelentano commented on GitHub (Nov 22, 2020):
The print are correct. I noticed an error that can be helpful:
Can it be an error with the Redirect URI? SPOTIPY_REDIRECT_URI enviroment variable and redirect URI on Spotify for Developers are the same (http://localhost:8080), but after the user give the authorization, often the site give "ERR_CONNECTION_REFUSED"
@Peter-Schorn commented on GitHub (Nov 22, 2020):
The reason for these errors is probably because the automatic authorization process does not work in the environment that your app is running in. What environment is it running in? Is it running on a server?
Yes, I think the issue is related to the redirect URI. It shouldn't be "http://localhost:8080" if the app is running on an external server. After you open the url from
SpotifyOAuth.get_authorize_url()and the user logs in to their Spotify account and taps agree, Spotify will redirect to the redirect URI that you specified. The redirect URI should somehow redirect back to your bot. For example, a callback function in your code should be executed after Spotify redirects back to your app. Do you know how to do this? If you don't, than I can't help you because I've never used the telegram bot API before.@alessiocelentano commented on GitHub (Nov 23, 2020):
It's running on my laptop with Arch Linux, so there shouldn't be problems, but the page with "Close" button is never loaded.
It is running on my PC and i read in the code that with localhost (and a port specified) I don't need to copy and paste the URL the browser is redirect to on the terminal.
What do you mean? What needs to be executed? I have a button that create a new spotipy.Spotify() instance for the user and store his data in a cache if the bot have the authorization, is not the same thing?
@Peter-Schorn commented on GitHub (Nov 23, 2020):
"OSError: [Errno 98] Address already in use" means that there is another process already using port 8080 (which is what appears in your redirect URI). You need to find that process and kill it. Alternatively, restart your computer. See https://stackoverflow.com/a/39557155/12394554
"spotipy.oauth2.SpotifyOauthError: Received error from auth server: access_denied" means that, after you opened the authorization URL in the browser and the user logged in to their Spotify account, the user tapped "cancel" instead of "agree", meaning that they denied your app access to their Spotify account.
It's hard for me to provide more help because I don't really understand how telegram bots work. You need to contact someone in the telegram community and ask them if it is possible to implement the OAuth authorization process, which is a widely recognized standard not specific to the Spotify web API.
@alessiocelentano commented on GitHub (Nov 24, 2020):
Okay, thanks for the help, I'll ask to someone in Telegram community to fix this problem (if possible)
@Peter-Schorn commented on GitHub (Nov 24, 2020):
Specifically, you need to ask if you can setup a callback URL that redirects back to your application.