New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to run entirely in a script? (no redirect url) #488
Comments
As opposed to the authorization code flow, you want the client credentials flow |
The docs tell me that |
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 #51 |
Hey, |
@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? |
Yes, it's opening a browser window. (Like, with the Spotify login thing). |
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 |
Yeah, I did it. |
Nope, it's in the "polybar scripts" directory |
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 |
Do you have the code for this polybar spotipy script? |
Actually, I have two of them. import sys
import spotipy
import spotipy.util as util
client_id = 'CLIENT_ID'
client_secret = 'CLIENT_SECRET'
redirect_uri = 'http://localhost/'
scope = 'user-read-currently-playing user-modify-playback-state'
token = spotipy.util.prompt_for_user_token(
'yha0gdu9143vclyk0cuqoro0m', scope, client_id, client_secret, redirect_uri)
def return_song():
if current_song != None and current_song != "Spotify":
print(current_song['item']['name'] + ' - ' + current_song['item']['artists'][0]['name'])
else:
if current_song == None:
print('No song currently playing')
else:
print(current_song)
if token:
sp = spotipy.Spotify(auth=token)
try:
current_song = sp.currently_playing()
except:
current_song = "Spotify"
return_song()
else:
print("Can't get token for", username) |
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 |
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) |
@PAK90 the token is refreshed automatically, so you don't need:
Also you don't need 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 |
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 |
@varna9000 make sure you use the latest version of spotipy: |
Ok, thanks. I've got the latest release. I'll open new issue. |
|
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:8000
url 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...
The text was updated successfully, but these errors were encountered: