mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 00:25:54 +03:00
[GH-ISSUE #446] Limited to one account ? #263
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#263
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 @AphroMad on GitHub (Feb 26, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/446
Hi !
I wanted to know,
can we use multiple account with the API ?
And if yes, how ?
thank you !
@stephanebruckert commented on GitHub (Feb 26, 2020):
I'm closing this because it is a duplicate of https://github.com/plamere/spotipy/issues/287. Feel free to subscribe to it and join the discussion.
We still need to document a proper example but so far the best example of it I found is the following:
https://github.com/plamere/spotipy/pull/435#issuecomment-583890341
@AphroMad commented on GitHub (Feb 28, 2020):
@stephanebruckert
it's been two days i'm trying to understand how those 2 can help to respond to my answer ..
can i have a hint ? ^^
Maybe my question wasn't clear enough.
Is this possible to connect and made actions with 2 or more accounts simultaneously ?
@AphroMad commented on GitHub (Feb 28, 2020):
Here is what i tried if i want to use 2 accounts, it doesn't work, i don't understand why, but maybe it's normal for you ^^
i'm a beginner with this api and i'm not an experimented programmer neither so i need help xD
And the thing that happen is, i think the connexion part works, but when i try to play the music, for the first time in the loop, it works with the first account.
But, in the second time in the loop, it works, but with the first account instead of the second, and i don't know why.
If somebody know, i'm listening because i'm lost ^^
@Peter-Schorn commented on GitHub (Mar 4, 2020):
I'm no expert on this either, so take what I say with a grain of salt, but, as I understand it util.prompt_for_user_token is just an inferior version of oauth_manager. This is because the former does not automatically refresh your token, whereas the latter does, so you should always use the latter.
Here is that way that I authenticate, and I think it's the simplest. I suggest you use this code as well.
@Peter-Schorn commented on GitHub (Mar 5, 2020):
@AphroMad If you want to work with multiple accounts, you must create a separate authentication manager for each user. I created a helper function that does that. Here it is, along with an example script. Please let me know if you have any questions. @stephanebruckert Could you please confirm that this information is correct?
@Peter-Schorn commented on GitHub (Mar 5, 2020):
@AphroMad Also, this code is invalid; it will raise a TypeError:
If you want to iterate through each element in a list, use the following syntax:
You also shouldn't use
objectas a variable name, it has a special meaning in certain contexts.In your version,
igets assigned to the each of the values of the list, souser_list[i]is equal touser_list["user_id_1"]and thenuser_list["user_id_2"]. When using square bracket notation for lists, you must pass in an integer or a slice (a section of a list).It also does not make sense to call the
str()function on an object that is already a string.@AphroMad commented on GitHub (Mar 5, 2020):
This isn't working, if i replace
` util.prompt_for_user_token(username,
scope,
client_id=clientID,
client_secret=clientsecret,
redirect_uri="http://google.com/")
by what you told me to use :
sp_oauth = spotipy.Spotify(auth_manager = spotipy.SpotifyOAuth(client_id = clientID, client_secret = clientsecret, redirect_uri = "http://google.com/", scope = scope, username = username))I got this : AttributeError: 'Spotify' object has no attribute 'get_access_token'
@Peter-Schorn commented on GitHub (Mar 5, 2020):
You may be using an earlier version of spotipy. Try upgrading. @AphroMad
@Peter-Schorn commented on GitHub (Mar 5, 2020):
@AphroMad Actually, you didn't copy my code properly. Take a closer look at what you wrote and what I wrote. They aren't the same. I highly suggest you directly copy and paste the code, rather than manually typing it out.
@AphroMad commented on GitHub (Mar 5, 2020):
Ok this work now, cool :)
i have to read your message about multiple accounts now :)
@AphroMad commented on GitHub (Mar 5, 2020):
Will it work if i use the same client_id and client_secret or is it needed to have one ID and secret per account ?
@stephanebruckert commented on GitHub (Mar 5, 2020):
@Peter-Schorn I haven't had time to try it but it looks correct and I believe this should work 👍
@Peter-Schorn commented on GitHub (Mar 5, 2020):
@AphroMad The way I tested it was with a separate client id and secret for each account. I'm not sure if this is necessary, however. I'll have to do some more testing.
@AphroMad commented on GitHub (Mar 5, 2020):
when i try, it works for one account but not for the second , i got a "INVALID_CLIENT: Invalid redirect URI" Error
Maybe this is because the client_id et client secret are the one of the first account
Or do you think this is for another reason ?
@Peter-Schorn commented on GitHub (Mar 5, 2020):
The way I did it was I logged in to the Spotify developer dashboard for user A and created an app, and then logged in to the Spotify developer dashboard for user B and created an app. Then I made sure that the usernames and the client ids and the client secrets corresponded to each other for the python script. Try that. Let me know if it works @AphroMad
@AphroMad commented on GitHub (Mar 5, 2020):
i've just tried, it doesn't work for me and i don't understand why
The error is the same than when i've one client id and one client secret for the two accounts.
When i launch the code, i have to enter the code with localhost, but on the page, it's written :
INVALID_CLIENT: Invalid redirect URI
And if i copy/paste the URL on spyder, i got this error :
User authentication requires interaction with your
web browser. Once you enter your credentials and
give authorization, you will be redirected to
a url. Paste that url you were directed to to
complete the authorization.
Opened https://accounts.spotify.com/authorize?client_id=381870e5f4ac4024b467c800bd29aed9&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F&scope=user-modify-playback-state in your browser
Enter the URL you were redirected to: https://accounts.spotify.com/authorize?client_id=381870e5f4ac4024b467c800bd29aed9&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F&scope=user-modify-playback-state
Traceback (most recent call last):
File "", line 1, in
runfile('D:/Prog&Job/Business/En cours/Spotify Bot/test_multicomptes.py', wdir='D:/Prog&Job/Business/En cours/Spotify Bot')
File "C:\Users\pierr\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\pierr\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/Prog&Job/Business/En cours/Spotify Bot/test_multicomptes.py", line 65, in
context_uri='spotify:playlist:37i9dQZF1DX1XDyq5cTk95'
File "C:\Users\pierr\Anaconda3\lib\site-packages\spotipy\client.py", line 1212, in start_playback
self._append_device_id("me/player/play", device_id), payload=data
File "C:\Users\pierr\Anaconda3\lib\site-packages\spotipy\client.py", line 237, in _put
return self._internal_call("PUT", url, payload, kwargs)
File "C:\Users\pierr\Anaconda3\lib\site-packages\spotipy\client.py", line 134, in _internal_call
headers = self._auth_headers()
File "C:\Users\pierr\Anaconda3\lib\site-packages\spotipy\client.py", line 124, in _auth_headers
token = self.auth_manager.get_access_token(as_dict=False)
File "C:\Users\pierr\Anaconda3\lib\site-packages\spotipy\oauth2.py", line 382, in get_access_token
raise SpotifyOauthError(response.reason)
SpotifyOauthError: Bad Request
@Peter-Schorn commented on GitHub (Mar 5, 2020):
@AphroMad On the line that says
Enter the URL you were redirected to:Did you type the same exact url as the one that was originally opened? If so you've made a mistake. You should be redirected to a URL beginning withhttp://localhost/and that's the one that you paste back into the script.If that wasn't the problem, then try the following steps:
Delete any and all cache files that currently exist. They should be in your home folder and match the following pattern:
.cache_yourUsername_user-modify-playback-stateNext, go to https://developer.spotify.com/dashboard/ and login as user A. Create a new client id and client secret, even if you've already created one before. Click on edit settings, and set
http://localhost/as the redirect URI.Then, logout of user A and login to user B. Repeat the same steps above. Keep track of which client id and secret corresponds each user.
Enter the new client ids and secrets into the python script. When you run it the first time, only enter the credentials for user A. Comment out the code that corresponds to user B. When you run the code, you should be redirected to a page that asks you to login with your spotify account, so you can authenticate the app. Make sure you are logged in as user A before you click AGREE.
Next, enter the credentials for user B and comment out the code corresponding to user A and repeat the same steps as above. MAKE SURE YOU ARE LOGGED IN AS USER B BEFORE YOU CLICK AGREE.
@Peter-Schorn commented on GitHub (Mar 5, 2020):
@AphroMad Ok, there is a way to use the same client id and secret for multiple Spotify accounts, but the process of setting it up is a little convoluted. Try it with 2 difference client ids and secrets first and let me know if you can get it to work.
@AphroMad commented on GitHub (Mar 6, 2020):
Ok, the big message to connect with 2 different client ID et client secret was usefull, now it works, thank you a lot !
So the 2 accounts can launch their music :)
So, let's go to the next step, how is it possible to use the same ClientID and clientSecret ?
@stephanebruckert commented on GitHub (Mar 12, 2020):
The following with
show_dialog=Trueworked for me:Your browser should now open twice, make sure to sign into
first_user_idandsecond_user_idrespectivelyResult:
@Peter-Schorn commented on GitHub (Mar 12, 2020):
@AphroMad You can also use this code to distinguish between the different users. It only uses one client id and secret, as you requested.
@AphroMad commented on GitHub (Mar 12, 2020):
I use the same solution since 2 or 3 days, the only difference between your code and mine is the show_dialog, i don't use/call it, what is it here for ?
@stephanebruckert commented on GitHub (Mar 12, 2020):
When using show_dialog=True the browser window that opens will allow you to sign out and select a different account than the one already signed in. Basically prevents you from clearing your browser cache manually when you plan to use a different account.
@AphroMad so is everything working now?
@AphroMad commented on GitHub (Mar 13, 2020):
I have to test with the show_dialog, then i'll post the code which works and i will close this subject :)
@AphroMad commented on GitHub (Mar 14, 2020):
Ok soooooo, here is the best solution for the initial problem.
Reminder, i wanted to connect and use multiple accounts and made them do something. Here, I made them play music !
Here is the code, which works, for this :
This works perfectly :)
Thank you again to @stephanebruckert and @Peter-Schorn !
@stephanebruckert commented on GitHub (Mar 14, 2020):
Excellent, thanks for pasting your solution
@Mau5trakt commented on GitHub (Jun 12, 2024):
Hey, how i can use it if i dont have the users_id and i wanna have it when they log in through my application?
I tryna do a Django App and allows multiple users, but i have no clue of how to get the users_id as you have it in an list.
@dieser-niko commented on GitHub (Jun 12, 2024):
Hi there, I'd recommend taking a look at
spotipy.cache_handlers.DjangoSessionCacheHandler.If you have any more questions, please open a separate issue as this one doesn't seem to be related to Django.