[GH-ISSUE #507] spotipy.oauth2.SpotifyOauthError: You must either set a cache_path or a username #304

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

Originally created by @Joshua-C-Clark on GitHub (Jun 1, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/507

I am trying to set up a web app and am first following your example app.py. I am running in to the following issues:

1.) SpotifyOauthError message for not passing a redirect_uri.
This was corrected by passing a url that has also been added to the Spotify Developer Dashboard.
auth_manager = spotipy.oauth2.SpotifyOAuth()
BECOMES
auth_manager = spotipy.oauth2.SpotifyOAuth(redirect_uri='http://localhost:5000/')

At this point I am able to open the app in my localhost and click the generated "Sign In" hyperlink. It loads the Spotify Authorize page, but when I click the "Agree" button I receive the following error:

spotipy.oauth2.SpotifyOauthError
spotipy.oauth2.SpotifyOauthError: You must either set a cache_path or a username.

I don't have a given user's username for the purpose of this app, since the user is supposed to be authenticating with Spotify and granting me scopes. Any examples on stackoverflow or your app.py are not passing a username.

Originally created by @Joshua-C-Clark on GitHub (Jun 1, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/507 I am trying to set up a web app and am first following your example app.py. I am running in to the following issues: 1.) SpotifyOauthError message for not passing a redirect_uri. This was corrected by passing a url that has also been added to the Spotify Developer Dashboard. auth_manager = spotipy.oauth2.SpotifyOAuth() BECOMES auth_manager = spotipy.oauth2.SpotifyOAuth(redirect_uri='http://localhost:5000/') At this point I am able to open the app in my localhost and click the generated "Sign In" hyperlink. It loads the Spotify Authorize page, but when I click the "Agree" button I receive the following error: spotipy.oauth2.SpotifyOauthError spotipy.oauth2.SpotifyOauthError: You must either set a cache_path or a username. I don't have a given user's username for the purpose of this app, since the user is supposed to be authenticating with Spotify and granting me scopes. Any examples on stackoverflow or your app.py are not passing a username.
kerem 2026-02-27 23:21:54 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@Joshua-C-Clark commented on GitHub (Jun 2, 2020):

Issue resolved by passing a cache file destination path:
sp_oauth = spotipy.oauth2.SpotifyOAuth(client_id=CLIENT_ID, client_secret=CLIENT_SECRET ,redirect_uri=REDIRECT_URI,scope=SCOPE, show_dialog=True, cache_path=CACHE)

Working through how to structure the caches files/folder for multiple users to be stored, as well as how to remove/delete a cache for a given user to allow for login/log off functionality to switch users logging in to the app.

<!-- gh-comment-id:637838743 --> @Joshua-C-Clark commented on GitHub (Jun 2, 2020): Issue resolved by passing a cache file destination path: `sp_oauth = spotipy.oauth2.SpotifyOAuth(client_id=CLIENT_ID, client_secret=CLIENT_SECRET ,redirect_uri=REDIRECT_URI,scope=SCOPE, show_dialog=True, cache_path=CACHE)` Working through how to structure the caches files/folder for multiple users to be stored, as well as how to remove/delete a cache for a given user to allow for login/log off functionality to switch users logging in to the app.
Author
Owner

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

Working through how to structure the caches files/folder for multiple users to be stored, as well as how to remove/delete a cache for a given user to allow for login/log off functionality to switch users logging in to the app.

See if this example can help https://github.com/plamere/spotipy/blob/master/examples/app.py, it allows multiple users to sign in at the same time. Currently it doesn't clear the tokens in the back-end, but that's something that can be improved

<!-- gh-comment-id:637879923 --> @stephanebruckert commented on GitHub (Jun 3, 2020): > Working through how to structure the caches files/folder for multiple users to be stored, as well as how to remove/delete a cache for a given user to allow for login/log off functionality to switch users logging in to the app. See if this example can help https://github.com/plamere/spotipy/blob/master/examples/app.py, it allows multiple users to sign in at the same time. Currently it doesn't clear the tokens in the back-end, but that's something that can be improved
Author
Owner

@MikeiLL commented on GitHub (Jul 6, 2020):

I don't know that this issue is resolved. When, in the App example, we instantiate spotipy.oauth2.SpotifyOAuth without specifying either a username or cache_path, the example returns an error, which you can see on line 274, elif not self.cache_path and not self.username it raises SpotifyOauthError, "You must either set a cache_path or a username.")

Personally, I am storing the credentials in a separate file, credentials.py, which I import and then instantiate like this:

CACHE = ".cache-" + "test"
SCOPE = None
# some possible scopes: user-top-read user-library-read playlist-read-public user-follow-read
auth_manager = spotipy.oauth2.SpotifyOAuth(credentials.SPOTIPY_CLIENT_ID, credentials.SPOTIPY_CLIENT_SECRET, credentials.SPOTIPY_REDIRECT_URI, scope=SCOPE, show_dialog=True, cache_path=CACHE)
spotipy.oauth2.SpotifyOAuth(credentials.SPOTIPY_CLIENT_ID, credentials.SPOTIPY_CLIENT_SECRET, credentials.SPOTIPY_REDIRECT_URI, scope=SCOPE, show_dialog=True, cache_path=CACHE)

This is a super-helpful demo, by the way. I think we can make it even more so.

<!-- gh-comment-id:654403120 --> @MikeiLL commented on GitHub (Jul 6, 2020): I don't know that this issue is resolved. When, in [the App example](https://github.com/plamere/spotipy/blob/master/examples/app.py), we instantiate `spotipy.oauth2.SpotifyOAuth` without specifying either a `username` or `cache_path`, the example returns an error, which you can see on line 274, `elif not self.cache_path and not self.username` it raises `SpotifyOauthError`, "You must either set a cache_path or a username.") Personally, I am storing the credentials in a separate file, `credentials.py`, which I import and then instantiate like this: ``` CACHE = ".cache-" + "test" SCOPE = None # some possible scopes: user-top-read user-library-read playlist-read-public user-follow-read auth_manager = spotipy.oauth2.SpotifyOAuth(credentials.SPOTIPY_CLIENT_ID, credentials.SPOTIPY_CLIENT_SECRET, credentials.SPOTIPY_REDIRECT_URI, scope=SCOPE, show_dialog=True, cache_path=CACHE) spotipy.oauth2.SpotifyOAuth(credentials.SPOTIPY_CLIENT_ID, credentials.SPOTIPY_CLIENT_SECRET, credentials.SPOTIPY_REDIRECT_URI, scope=SCOPE, show_dialog=True, cache_path=CACHE) ``` This is a super-helpful demo, by the way. I think we can make it even more so.
Author
Owner

@stephanebruckert commented on GitHub (Jul 6, 2020):

I personally can't reproduce it but I've reopened the issue

<!-- gh-comment-id:654404335 --> @stephanebruckert commented on GitHub (Jul 6, 2020): I personally can't reproduce it but I've reopened the issue
Author
Owner

@MikeiLL commented on GitHub (Jul 6, 2020):

Thanks. I'm having another problem, which is that current_user_playing_track and currently_playing, called without arguments:

@app.route('/currently_playing')
def current_playing_track():
	if not session.get('token_info'):
		return redirect('/')
	else:
		return spotify.currently_playing()   

raise a "Permissions missing" exception:

spotipy.exceptions.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/me/player/currently-playing:
 Permissions missing

Looking at the _internal_call that _get points to but I'm not sure if I should be sending something to args, payload, or **kwargs.

I expect it's my own ignorance, rather than a bug. Would you like me to create a new issue? Run to Stack Overflow?

Thanks, man.

<!-- gh-comment-id:654410107 --> @MikeiLL commented on GitHub (Jul 6, 2020): Thanks. I'm having another problem, which is that [current_user_playing_track](https://github.com/plamere/spotipy/blob/master/spotipy/client.py#L919) and [currently_playing](https://github.com/plamere/spotipy/blob/master/spotipy/client.py#L1349), called without arguments: ``` @app.route('/currently_playing') def current_playing_track(): if not session.get('token_info'): return redirect('/') else: return spotify.currently_playing() ``` raise a "Permissions missing" exception: ``` spotipy.exceptions.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/me/player/currently-playing: Permissions missing ``` Looking at the [_internal_call](https://github.com/plamere/spotipy/blob/master/spotipy/client.py#L211) that `_get` points to but I'm not sure if I should be sending something to `args`, `payload`, or `**kwargs`. I expect it's my own ignorance, rather than a bug. Would you like me to create a new issue? Run to Stack Overflow? Thanks, man.
Author
Owner

@stephanebruckert commented on GitHub (Jul 6, 2020):

@MikeiLL I've never seen that one, please create a new issue with your full code

<!-- gh-comment-id:654416702 --> @stephanebruckert commented on GitHub (Jul 6, 2020): @MikeiLL I've never seen that one, please create a new issue with your full code
Author
Owner

@MikeiLL commented on GitHub (Jul 6, 2020):

Done. Thank you.

<!-- gh-comment-id:654424501 --> @MikeiLL commented on GitHub (Jul 6, 2020): [Done](https://github.com/plamere/spotipy/issues/530). Thank you.
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#304
No description provided.