mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-26 16:15:51 +03:00
[GH-ISSUE #488] How to run entirely in a script? (no redirect url) #288
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#288
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 @PAK90 on GitHub (May 2, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/488
I'm trying to use this library to get recently played information from the API, and I will want this script to run automatically without any user intervention. Is it possible to do that with this library? Right now this is my code:
However when I run this it tries to open a window in my browser to get authentication working. The
localhost:8000url is fake, I don't actually have anything there, I just put it in so it would stop complaining about not receiving a redirect url.What's the way to have this run without requiring manual intervention? I need this script to run by itself entirely. It seems that having the ID and secret should be enough...
@stephanebruckert commented on GitHub (May 2, 2020):
As opposed to the authorization code flow, you want the client credentials flow
@PAK90 commented on GitHub (May 2, 2020):
The docs tell me that
Only endpoints that do not access user information can be accessed.I specifically want to access my user details, specifically which songs have been played. Is the documentation wrong and I will be able to access this info?@stephanebruckert commented on GitHub (May 3, 2020):
Sorry I didn't see you needed user info. In that case you have to let the script prompt for sign in at least once so it can retrieve the token. The script should then be able to use the cached token and run without human intervention.
Currently the token is cached in a file. Different ways of persisting this token are discussed here https://github.com/plamere/spotipy/issues/51
@busybox11 commented on GitHub (May 4, 2020):
Hey,
I'm using the same method on my daily driver.
I'm using spotipy to get the currently playing track and to control it on a polybar module that I created myself.
However, when I log on my computer, a browser window is always appearing with the
localhosturl and Spotify's code.How to stop this behaviour ?
@stephanebruckert commented on GitHub (May 4, 2020):
@busybox11 interesting, do you have the code under hand? what exact URL does it open (do you have a screenshot)?
I assume that it's opening a browser window because it doesn't have a token, and your script needs a token to run. Do you know where the token is kept? Is it not persisted between computer sessions?
@busybox11 commented on GitHub (May 4, 2020):
Yes, it's opening a browser window. (Like, with the Spotify login thing).
I used a workaround that someone posted on Mano other issues (I use crontab to copy the cache file)
I can't send a screenshot since it's an almost-headless script (it can run apps tho, but it doesn't have an output)
@stephanebruckert commented on GitHub (May 4, 2020):
You shouldn't need crontab. A same token, once generated, can be refreshed an unlimited amount of time.
So just run your script outside of polybar once and you should be good after signing in
@busybox11 commented on GitHub (May 4, 2020):
Yeah, I did it.
But after each reboot (I don't really know but it's something like this) the cache file isn't being used anymore
@busybox11 commented on GitHub (May 4, 2020):
Nope, it's in the "polybar scripts" directory
@stephanebruckert commented on GitHub (May 4, 2020):
Can you please check if the cached file is still there?
Edit: deleted my previous answer and you answered faster than me. Hmm ok, then you are right it shouldn't prompt for another signin
@stephanebruckert commented on GitHub (May 4, 2020):
Do you have the code for this polybar spotipy script?
@busybox11 commented on GitHub (May 4, 2020):
Actually, I have two of them.
This is the script used to display the song currently being played.
@stephanebruckert commented on GitHub (May 4, 2020):
this is a util that should not be used "in production". It should be ran "locally" only to generate the token and can only be ran once.
Instead of:
Do this:
Hope that fixes it
@PAK90 commented on GitHub (May 5, 2020):
So I ran this:
After doing this successfully, I got a local .cache file. However, I don't know how to use this and refresh it accordingly? The docs also don't say. From your response above, I tried doing this:
However I get a 400 Client Error: Bad Request for url: https://api.spotify.com/v1/users/userstring, Only valid bearer authentication supported.
How do I do this properly? (to be clear, I have my python file and the .cache file in the same directory)
@stephanebruckert commented on GitHub (May 5, 2020):
@PAK90 the token is refreshed automatically, so you don't need:
Also you don't need
util.prompt_for_user_tokenanymore, just dospotipy.Spotify(client_credentials_manager=creds).Lately there has been a lot of changes around how we get the first token and refresh it. The doc might still need a round of updates to reflect that by deprecating
util.prompt_for_user_token@varna9000 commented on GitHub (May 17, 2020):
H @stephanebruckert , do you know why the current playing song method hangs (actually all sp methods hang the code) ? The token seems to be got ok, as the line which hags the code is calling the currently_playing() method. Basically I'm using the
spotipy.Spotify(client_credentials_manager=creds)auth method which you recommend here.@stephanebruckert commented on GitHub (May 17, 2020):
@varna9000 make sure you use the latest version of spotipy:
pip install spotipy --upgrade. If the problem persists, please create a new issue with a minimal working example@varna9000 commented on GitHub (May 17, 2020):
Ok, thanks. I've got the latest release. I'll open new issue.
@stephanebruckert commented on GitHub (Jun 14, 2020):
util.prompt_for_user_tokenis now deprecated and the docs now suggest to use the auth manager instead. Closing as all questions here should be answered