[GH-ISSUE #441] "You must either set a cache_path or a username" when using get_access_token #256

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

Originally created by @CharleyPearce on GitHub (Feb 15, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/441

I am using the oauth for the login system to my app, however I'm not providing a cache path because I am using a different form of caching the token info on my server.

When a user is redirected back from Spotify, I use sp_oauth.get_access_token(code) to verify the code. When this happens, I get the following error:

SpotifyOauthError at /app/
You must either set a cache_path or a username.

This is because get_access_token calls get_cached_token, which then throws an error when a cache path is not found.

Is it possible that I can call get_access_token to verify a code from Spotify without needing a specified cache path?

Originally created by @CharleyPearce on GitHub (Feb 15, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/441 I am using the oauth for the login system to my app, however I'm not providing a cache path because I am using a different form of caching the token info on my server. When a user is redirected back from Spotify, I use `sp_oauth.get_access_token(code)` to verify the code. When this happens, I get the following error: ``` SpotifyOauthError at /app/ You must either set a cache_path or a username. ``` This is because `get_access_token` calls `get_cached_token`, which then throws an error when a cache path is not found. Is it possible that I can call `get_access_token` to verify a code from Spotify without needing a specified cache path?
kerem 2026-02-27 23:21:39 +03:00
Author
Owner

@stephanebruckert commented on GitHub (Feb 15, 2020):

How are you creating the sp_oauth object at first? To provide the username, you should be able to:

  • either create sp_oauth by doing something like:

    sp_oauth = spotipy.SpotifyOAuth(username=your_username, etc)
    
  • or by setting an environment variable SPOTIPY_CLIENT_USERNAME

Then you won't need to pass a cache path anymore as it should be generated using your username

<!-- gh-comment-id:586588279 --> @stephanebruckert commented on GitHub (Feb 15, 2020): How are you creating the `sp_oauth` object at first? To provide the username, you should be able to: - either create `sp_oauth` by doing something like: sp_oauth = spotipy.SpotifyOAuth(username=your_username, etc) - or by setting an environment variable `SPOTIPY_CLIENT_USERNAME` Then you won't need to pass a cache path anymore as it should be generated using your username
Author
Owner

@CharleyPearce commented on GitHub (Feb 15, 2020):

I don't specify the username as when I define my sp_oauth object_object, I don't know the user's username at that point, so my definition will look something like this:

sp_oauth = oauth2.SpotifyOAuth( client_id, client_secret,redirect_uri,scope=scope)
<!-- gh-comment-id:586596286 --> @CharleyPearce commented on GitHub (Feb 15, 2020): I don't specify the username as when I define my sp_oauth object_object, I don't know the user's username at that point, so my definition will look something like this: ``` sp_oauth = oauth2.SpotifyOAuth( client_id, client_secret,redirect_uri,scope=scope) ```
Author
Owner

@stephanebruckert commented on GitHub (Feb 15, 2020):

Ha, I think I see. Currently the code is coupled with the fact that we want to cache the token as a file. The good news is we can work on changing that.

First, can I suggest something as a temporary workaround? Can you try to provide a random cache_path or username:

 import random
 import string
 random = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(32)])

 oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, scope=scope,
                     cache_path="/tmp/.cache-"+random)]

This will create a file that you will never use. It should allow you to retrieve the token using the code. Then I suppose you are storing this token somewhere else on your server, and the next time you want to use it, you could just do sp = spotipy.Spotify(auth=token).

Let me know how this goes. If this works for you, we might be able to remove this warning and make a version that allows working without cache, or better, with custom caches.

<!-- gh-comment-id:586600622 --> @stephanebruckert commented on GitHub (Feb 15, 2020): Ha, I think I see. Currently the code is coupled with the fact that we want to cache the token as a file. The good news is we can work on changing that. First, can I suggest something as a temporary workaround? Can you try to provide a random cache_path or username: import random import string random = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(32)]) oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, scope=scope, cache_path="/tmp/.cache-"+random)] This will create a file that you will never use. It should allow you to retrieve the token using the code. Then I suppose you are storing this token somewhere else on your server, and the next time you want to use it, you could just do `sp = spotipy.Spotify(auth=token)`. Let me know how this goes. If this works for you, we might be able to remove this warning and make a version that allows working without cache, or better, with custom caches.
Author
Owner

@stephanebruckert commented on GitHub (Feb 15, 2020):

Released in 2.9.0, thanks for the quick & simple PR!

<!-- gh-comment-id:586635566 --> @stephanebruckert commented on GitHub (Feb 15, 2020): Released in [2.9.0](https://github.com/plamere/spotipy/releases/tag/2.9.0), thanks for the quick & simple PR!
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#256
No description provided.