[GH-ISSUE #488] How to run entirely in a script? (no redirect url) #288

Closed
opened 2026-02-27 23:21:48 +03:00 by kerem · 19 comments
Owner

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:

from spotipy.oauth2 import SpotifyOAuth
token = SpotifyOAuth(
	SPOTIFY_CLIENT_ID,
	SPOTIFY_CLIENT_SECRET,
	'localhost:8000',
	scope='user-read-recently-played',
	username='vacd7qnn09sx0dtj56qtwmix7'
	)

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...

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: from spotipy.oauth2 import SpotifyOAuth token = SpotifyOAuth( SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, 'localhost:8000', scope='user-read-recently-played', username='vacd7qnn09sx0dtj56qtwmix7' ) 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...
kerem 2026-02-27 23:21:48 +03:00
Author
Owner

@stephanebruckert commented on GitHub (May 2, 2020):

As opposed to the authorization code flow, you want the client credentials flow

<!-- gh-comment-id:622929568 --> @stephanebruckert commented on GitHub (May 2, 2020): As opposed to the [authorization code flow](https://spotipy.readthedocs.io/en/2.12.0/#authorization-code-flow), you want the [client credentials flow](https://spotipy.readthedocs.io/en/2.12.0/#client-credentials-flow)
Author
Owner

@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?

<!-- gh-comment-id:623005559 --> @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?
Author
Owner

@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

<!-- gh-comment-id:623033971 --> @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
Author
Owner

@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 localhost url and Spotify's code.
How to stop this behaviour ?

<!-- gh-comment-id:623337271 --> @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 `localhost` url and Spotify's code. How to stop this behaviour ?
Author
Owner

@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?

<!-- gh-comment-id:623351571 --> @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?
Author
Owner

@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)

<!-- gh-comment-id:623399364 --> @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)
Author
Owner

@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

<!-- gh-comment-id:623574837 --> @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
Author
Owner

@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

<!-- gh-comment-id:623583768 --> @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
Author
Owner

@busybox11 commented on GitHub (May 4, 2020):

Nope, it's in the "polybar scripts" directory

<!-- gh-comment-id:623585978 --> @busybox11 commented on GitHub (May 4, 2020): Nope, it's in the "polybar scripts" directory
Author
Owner

@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

<!-- gh-comment-id:623586080 --> @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
Author
Owner

@stephanebruckert commented on GitHub (May 4, 2020):

Do you have the code for this polybar spotipy script?

<!-- gh-comment-id:623587503 --> @stephanebruckert commented on GitHub (May 4, 2020): Do you have the code for this polybar spotipy script?
Author
Owner

@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.


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)
<!-- gh-comment-id:623591473 --> @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. ```PYTHON 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) ```
Author
Owner

@stephanebruckert commented on GitHub (May 4, 2020):

util.prompt_for_user_token(
    'yha0gdu9143vclyk0cuqoro0m', scope, client_id, client_secret, redirect_uri)

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:

token = spotipy.util.prompt_for_user_token(
   'yha0gdu9143vclyk0cuqoro0m', scope, client_id, client_secret, redirect_uri)
sp = spotipy.Spotify(auth=token)

Do this:

from spotipy import SpotifyOAuth

creds = SpotifyOAuth(scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
sp = spotipy.Spotify(auth_manager=creds)

Hope that fixes it

<!-- gh-comment-id:623598231 --> @stephanebruckert commented on GitHub (May 4, 2020): util.prompt_for_user_token( 'yha0gdu9143vclyk0cuqoro0m', scope, client_id, client_secret, redirect_uri) 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: token = spotipy.util.prompt_for_user_token( 'yha0gdu9143vclyk0cuqoro0m', scope, client_id, client_secret, redirect_uri) sp = spotipy.Spotify(auth=token) Do this: from spotipy import SpotifyOAuth creds = SpotifyOAuth(scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri) sp = spotipy.Spotify(auth_manager=creds) Hope that fixes it
Author
Owner

@PAK90 commented on GitHub (May 5, 2020):

So I ran this:

token = util.prompt_for_user_token(
	'userstring'
	'user-read-recently-played',
	client_id=SPOTIFY_CLIENT_ID,
	client_secret=SPOTIFY_CLIENT_SECRET,
	redirect_uri='http://localhost:8080',
	)

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:

creds = SpotifyOAuth(
	client_id=SPOTIFY_CLIENT_ID,
	client_secret=SPOTIFY_CLIENT_SECRET,
	redirect_uri='http://localhost:8080',
	scope='user-read-recently-played',
	)
token = creds.get_cached_token()
sp = spotipy.Spotify(auth=token)
user = sp.user('userstring')
print(sp.current_user_recently_played())

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)

<!-- gh-comment-id:623813641 --> @PAK90 commented on GitHub (May 5, 2020): So I ran this: token = util.prompt_for_user_token( 'userstring' 'user-read-recently-played', client_id=SPOTIFY_CLIENT_ID, client_secret=SPOTIFY_CLIENT_SECRET, redirect_uri='http://localhost:8080', ) 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: creds = SpotifyOAuth( client_id=SPOTIFY_CLIENT_ID, client_secret=SPOTIFY_CLIENT_SECRET, redirect_uri='http://localhost:8080', scope='user-read-recently-played', ) token = creds.get_cached_token() sp = spotipy.Spotify(auth=token) user = sp.user('userstring') print(sp.current_user_recently_played()) 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)
Author
Owner

@stephanebruckert commented on GitHub (May 5, 2020):

@PAK90 the token is refreshed automatically, so you don't need:

token = creds.get_cached_token()

Also you don't need util.prompt_for_user_token anymore, just do spotipy.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

<!-- gh-comment-id:623932564 --> @stephanebruckert commented on GitHub (May 5, 2020): @PAK90 the token is refreshed automatically, so you don't need: token = creds.get_cached_token() Also you don't need `util.prompt_for_user_token` anymore, just do `spotipy.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`
Author
Owner

@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.

<!-- gh-comment-id:629774934 --> @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.
Author
Owner

@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

<!-- gh-comment-id:629782371 --> @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
Author
Owner

@varna9000 commented on GitHub (May 17, 2020):

Ok, thanks. I've got the latest release. I'll open new issue.

<!-- gh-comment-id:629786935 --> @varna9000 commented on GitHub (May 17, 2020): Ok, thanks. I've got the latest release. I'll open new issue.
Author
Owner

@stephanebruckert commented on GitHub (Jun 14, 2020):

util.prompt_for_user_token is now deprecated and the docs now suggest to use the auth manager instead. Closing as all questions here should be answered

<!-- gh-comment-id:643794352 --> @stephanebruckert commented on GitHub (Jun 14, 2020): `util.prompt_for_user_token` is now deprecated and the docs now suggest to use the auth manager instead. Closing as all questions here should be answered
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#288
No description provided.