[GH-ISSUE #470] Writing cache file requires code to be run in administrator mode - Windows 10 #276

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

Originally created by @ghost on GitHub (Apr 12, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/470

When I run the default code

import spotipy

import spotipy.util as util

scope = 'user-library-read user-read-playback-state user-modify-playback-state'

username = MY_USERNAME

token = util.prompt_for_user_token(username,
                                   scope,
                                   client_id=MY_CLIENT_ID,
                                   client_secret=MY_CLIENT_SECRET,
                                   redirect_uri='http://localhost:8080/',
                                  )

sp = spotipy.Spotify(auth=token)

sp.trace = True  # turn on tracing
sp.trace_out = True  # turn on trace out
# sp.start_playback()
user = sp.me()

print(user)

On Windows 10, I get the warning: 'Couldn't write token to cache at: %s'...
When I ran the pycharm IDE in administrator mode, it worked, I thought this could be fixed by manually defining a cache_path parameter of the util.prompt_for_user_token function to a location that I could write without administrator mode, but defining the cache_path always throws me the following trace:

Traceback (most recent call last):
  File "c:\python38-32\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\python38-32\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\rodri\Documents\Projetos\SpotipyImplementation\main.py", line 9, in <module>
    token = util.prompt_for_user_token(username,
  File "C:\Users\rodri\.virtualenvs\SpotipyImplementation-4ZHTFhBf\lib\site-packages\spotipy\util.py", line 88, in prompt_for_user_token
    token_info = sp_oauth.get_cached_token()
  File "C:\Users\rodri\.virtualenvs\SpotipyImplementation-4ZHTFhBf\lib\site-packages\spotipy\oauth2.py", line 253, in get_cached_token
    token_info = json.loads(token_info_string)
  File "c:\python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "c:\python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\python38-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 1

I was thinking about submitting a pull request that when the cache_path is an empty/non-existing file, it could create it. Is that okay for you?

Originally created by @ghost on GitHub (Apr 12, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/470 When I run the default code ``` import spotipy import spotipy.util as util scope = 'user-library-read user-read-playback-state user-modify-playback-state' username = MY_USERNAME token = util.prompt_for_user_token(username, scope, client_id=MY_CLIENT_ID, client_secret=MY_CLIENT_SECRET, redirect_uri='http://localhost:8080/', ) sp = spotipy.Spotify(auth=token) sp.trace = True # turn on tracing sp.trace_out = True # turn on trace out # sp.start_playback() user = sp.me() print(user) ``` On Windows 10, I get the warning: 'Couldn\'t write token to cache at: %s'... When I ran the pycharm IDE in administrator mode, it worked, I thought this could be fixed by manually defining a cache_path parameter of the util.prompt_for_user_token function to a location that I could write without administrator mode, but defining the cache_path always throws me the following trace: ``` Traceback (most recent call last): File "c:\python38-32\lib\runpy.py", line 193, in _run_module_as_main return _run_code(code, main_globals, None, File "c:\python38-32\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\rodri\Documents\Projetos\SpotipyImplementation\main.py", line 9, in <module> token = util.prompt_for_user_token(username, File "C:\Users\rodri\.virtualenvs\SpotipyImplementation-4ZHTFhBf\lib\site-packages\spotipy\util.py", line 88, in prompt_for_user_token token_info = sp_oauth.get_cached_token() File "C:\Users\rodri\.virtualenvs\SpotipyImplementation-4ZHTFhBf\lib\site-packages\spotipy\oauth2.py", line 253, in get_cached_token token_info = json.loads(token_info_string) File "c:\python38-32\lib\json\__init__.py", line 357, in loads return _default_decoder.decode(s) File "c:\python38-32\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "c:\python38-32\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Process finished with exit code 1 ``` I was thinking about submitting a pull request that when the cache_path is an empty/non-existing file, it could create it. Is that okay for you?
kerem closed this issue 2026-02-27 23:21:45 +03:00
Author
Owner

@stephanebruckert commented on GitHub (Apr 12, 2020):

Have you also tried running your cmd.exe in administrator mode?

<!-- gh-comment-id:612671070 --> @stephanebruckert commented on GitHub (Apr 12, 2020): Have you also tried running your `cmd.exe` in administrator mode?
Author
Owner

@ghost commented on GitHub (Apr 12, 2020):

It also does work, but what do you think about the option of creating the file on the cache_path, so the administrator mode is not required? I'm willing to do that if you accept it.

<!-- gh-comment-id:612679526 --> @ghost commented on GitHub (Apr 12, 2020): It also does work, but what do you think about the option of creating the file on the cache_path, so the administrator mode is not required? I'm willing to do that if you accept it.
Author
Owner

@stephanebruckert commented on GitHub (Apr 13, 2020):

I think you are referring to this particular problem https://github.com/plamere/spotipy/issues/176#issuecomment-307632735 where it won't work if you run util. prompt_for_user_token twice. It should work however if you delete your cached token manually and then run the code again. I agree that it's not OK and if you can solve this in a PR it's definitely welcome! 🙏

<!-- gh-comment-id:612864203 --> @stephanebruckert commented on GitHub (Apr 13, 2020): I think you are referring to this particular problem https://github.com/plamere/spotipy/issues/176#issuecomment-307632735 where it won't work if you run `util. prompt_for_user_token` twice. It should work however if you delete your cached token manually and then run the code again. I agree that it's not OK and if you can solve this in a PR it's definitely welcome! 🙏
Author
Owner

@ghost commented on GitHub (Apr 14, 2020):

So I was trying to replicate the error you referenciated, and had no success on it, I think this problem has already been solved.
And for some reason, I also cannot replicate my own error either, so I guess we can close this issue.
Thank you for your help.

<!-- gh-comment-id:613174729 --> @ghost commented on GitHub (Apr 14, 2020): So I was trying to replicate the error you referenciated, and had no success on it, I think this problem has already been solved. And for some reason, I also cannot replicate my own error either, so I guess we can close this issue. Thank you for your help.
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#276
No description provided.