[GH-ISSUE #363] warning:couldn't write token cache to .cache-xxxxxxxxxx #213

Open
opened 2026-02-27 23:21:24 +03:00 by kerem · 14 comments
Owner

Originally created by @sifus on GitHub (Mar 27, 2019).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/363

Originally created by @sifus on GitHub (Mar 27, 2019). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/363
Author
Owner

@sifus commented on GitHub (Mar 27, 2019):

Hello, i can't go furether because my token can't go to the cache.

Is someone have a solution ...

<!-- gh-comment-id:476924401 --> @sifus commented on GitHub (Mar 27, 2019): Hello, i can't go furether because my token can't go to the cache. Is someone have a solution ...
Author
Owner

@TineshaErskine commented on GitHub (Apr 8, 2019):

Did you ever figure this out?
I'm running into the same issue. I'm running code to look at user's top artist.

I get:
'warning:couldn't write token cache to .cache-C'

and a link to a page that states:
{
"error": {
"status": 401,
"message": "No token provided"
}
}

I have read that a lot of people were having trouble with the token part but I double checked and it seems right:

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

image

<!-- gh-comment-id:481011441 --> @TineshaErskine commented on GitHub (Apr 8, 2019): Did you ever figure this out? I'm running into the same issue. I'm running code to look at user's top artist. I get: '**warning:couldn't write token cache to .cache-C**' and a link to a page that states: **{ "error": { "status": 401, "message": "No token provided" } }** I have read that a lot of people were having trouble with the token part but I double checked and it seems right: **'token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri) sp = spotipy.Spotify(auth=token)'** ![image](https://user-images.githubusercontent.com/45402779/55757314-a0d14280-5a21-11e9-8a0b-ce8f7c3d4a34.png)
Author
Owner

@HurleybirdJr commented on GitHub (May 11, 2020):

I may have a solution.

Problem:

On Windows 10, Windows Defender tends to block new programs from writing to personal C:/ areas (Documents, Pictures, etc...) with "Ransomware protection" and with proper error catching, would normally say the file/folder doesn't exist.

Solution:

What I did was:

  • Go into Windows Defender, under "Virus & threat protection"
  • Then "Ransomware protection" into "Allow an app through controlled folder access".

Assuming you're the administrator:

  • Go to "Add an allowed app" then "Recently blocked apps"
  • Then a list of programs and their respective directories should appear. For me "pythonw.exe" was listed at the top.
  • Then, you can go ahead and add it to the whitelist and if you rerun your program, it should work without that error popping up again...

Hope that helps :)

<!-- gh-comment-id:626980728 --> @HurleybirdJr commented on GitHub (May 11, 2020): I may have a solution. ### Problem: On Windows 10, Windows Defender tends to block new programs from writing to personal C:/ areas (Documents, Pictures, etc...) with "**_Ransomware protection_**" and with proper error catching, would normally say the file/folder doesn't exist. ### Solution: What I did was: - Go into Windows Defender, under "**_Virus & threat protection_**" - Then "**_Ransomware protection_**" into "**_Allow an app through controlled folder access_**". Assuming you're the administrator: - Go to "**_Add an allowed app_**" then "**_Recently blocked apps_**" - Then a list of programs and their respective directories should appear. For me "**_pythonw.exe_**" was listed at the top. - Then, you can go ahead and add it to the whitelist and if you rerun your program, it should work without that error popping up again... Hope that helps :)
Author
Owner

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

@HurleybirdJr nice, we can add that to the FAQ

<!-- gh-comment-id:626999509 --> @stephanebruckert commented on GitHub (May 11, 2020): @HurleybirdJr nice, we can add that to the FAQ
Author
Owner

@janecafe commented on GitHub (Jun 20, 2020):

I am having a similar problem myself. I am trying to run my .py script with a batch file on my Windows command line. After it authorizes my Spotify and I enter the URl I was directed to, I get this output:
Couldn't write token to cache at: .cache-{{my spotify username}}
Usage: {the path where my file is} {my spotify username}
*

I checked my ransomware on the 3rd party security I use and python.exe is an allowed program.

<!-- gh-comment-id:646917324 --> @janecafe commented on GitHub (Jun 20, 2020): I am having a similar problem myself. I am trying to run my .py script with a batch file on my Windows command line. After it authorizes my Spotify and I enter the URl I was directed to, I get this output: *Couldn't write token to cache at: .cache-{{my spotify username}} Usage: {the path where my file is} {my spotify username}** I checked my ransomware on the 3rd party security I use and python.exe is an allowed program.
Author
Owner

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

@jane-dempsey is the same happening if you try to run the python file directly, without the help of a batch file? Perhaps, the batch file doesn't have sufficient authorization and would need to be added to the list of allowed programs, or it should be ran as an administrator

<!-- gh-comment-id:646964393 --> @stephanebruckert commented on GitHub (Jun 20, 2020): @jane-dempsey is the same happening if you try to run the python file directly, without the help of a batch file? Perhaps, the batch file doesn't have sufficient authorization and would need to be added to the list of allowed programs, or it should be ran as an administrator
Author
Owner

@janecafe commented on GitHub (Jun 20, 2020):

@stephanebruckert I tried the following:

  • tried to add my .py file and .bat file to list of allowed program in 3rd party security system; was unable to do so.
  • temporarily turned off my 3rd party security system to see if it was blocking it. try to run by .bat and .py files and ran into same issues.
  • ran my .bat file and . py files as administrator. same issue arose.
  • when I try to run the files, A .cache (?) file gets saved onto my desktop labelled .cache-{my username}. I frequently delete this file to "clear" whatever spotipy cache is put onto my computer.

If it is useful, here is my intial start-up code:

import os
import sys
import spotipy
import spotipy.util as util
import pandas as pd
import numpy as np
username = '{my username}'
scope = 'user-library-read'
redirect_uri = 'http://localhost/'
CLIENT_ID = '{my id}'
CLIENT_SECRET = '{my secret}'
token = util.prompt_for_user_token(username, scope, CLIENT_ID, CLIENT_SECRET, redirect_uri)
sp = spotipy.Spotify(auth=token)

It's at this point the .py and .bat files open up a new tab in Google Chrome and I do the same routine and pasting the URL into the command line. After that is when the command line either closes spontaneously or I get the above message I mentioned.
If it's useful, I have a previous version that does work with the difference being the scope: 'user-read-currently-playing'. However, I need the scope 'user-library-read' to implement part of my current code. I hope these details were helpful! And I'm definitely trying various methods!

<!-- gh-comment-id:647005357 --> @janecafe commented on GitHub (Jun 20, 2020): @stephanebruckert I tried the following: - tried to add my .py file and .bat file to list of allowed program in 3rd party security system; was unable to do so. - temporarily turned off my 3rd party security system to see if it was blocking it. try to run by .bat and .py files and ran into same issues. - ran my .bat file and . py files as administrator. same issue arose. - when I try to run the files, A .cache (?) file gets saved onto my desktop labelled .cache-{my username}. I frequently delete this file to "clear" whatever spotipy cache is put onto my computer. If it is useful, here is my intial start-up code: ``` import os import sys import spotipy import spotipy.util as util import pandas as pd import numpy as np username = '{my username}' scope = 'user-library-read' redirect_uri = 'http://localhost/' CLIENT_ID = '{my id}' CLIENT_SECRET = '{my secret}' token = util.prompt_for_user_token(username, scope, CLIENT_ID, CLIENT_SECRET, redirect_uri) sp = spotipy.Spotify(auth=token) ``` It's at this point the .py and .bat files open up a new tab in Google Chrome and I do the same routine and pasting the URL into the command line. After that is when the command line either closes spontaneously or I get the above message I mentioned. If it's useful, I have a previous version that does work with the difference being the scope: 'user-read-currently-playing'. However, I need the scope 'user-library-read' to implement part of my current code. I hope these details were helpful! And I'm definitely trying various methods!
Author
Owner

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

Also try to run your command line (cmd.exe?) as administrator. I don't know why Windows would need this but I'm hoping to unblock you at the moment.

And, have to say it's really weird that it only happens with specific scopes...

<!-- gh-comment-id:647046288 --> @stephanebruckert commented on GitHub (Jun 20, 2020): Also try to run your command line (cmd.exe?) as administrator. I don't know why Windows would need this but I'm hoping to unblock you at the moment. And, have to say it's really weird that it only happens with specific scopes...
Author
Owner

@janecafe commented on GitHub (Jun 21, 2020):

@stephanebruckert I tried running my cmd line as admin, as well as my .bat file and Python 3.8. Same error.

I figured it out! It was a line in my code :) Thank you!!

<!-- gh-comment-id:647140817 --> @janecafe commented on GitHub (Jun 21, 2020): @stephanebruckert I tried running my cmd line as admin, as well as my .bat file and Python 3.8. Same error. I figured it out! It was a line in my code :) Thank you!!
Author
Owner

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

What was it exactly? Please share all details as it will definitely be useful for others!

<!-- gh-comment-id:647164267 --> @stephanebruckert commented on GitHub (Jun 21, 2020): What was it exactly? Please share all details as it will definitely be useful for others!
Author
Owner

@janecafe commented on GitHub (Jun 21, 2020):

Sure did two things, so for future references users can try both:

  1. In my .bat file I put an -i in front of the .py script path so python -i script.py (source: https://stackoverflow.com/questions/12375173/how-to-stop-python-closing-immediately-when-executed-in-microsoft-windows)
  2. I re-read my code which had the following, which I commented out and the code is running fine without it. This was a part of a coding block that creates a dataframe from a Spotify playlist, but my personal Spotify has already been authorized previously using the standard Spotipy authorization flow.
if len(sys.argv) > 1:
    username = sys.argv[1]
else:
    print('Usage: %s {myusername}' % (sys.argv[0],))
    sys.exit()

<!-- gh-comment-id:647165687 --> @janecafe commented on GitHub (Jun 21, 2020): Sure did two things, so for future references users can try both: 1. In my .bat file I put an -i in front of the .py script path so `python -i script.py` (source: https://stackoverflow.com/questions/12375173/how-to-stop-python-closing-immediately-when-executed-in-microsoft-windows) 2. I re-read my code which had the following, which I commented out and the code is running fine without it. This was a part of a coding block that creates a dataframe from a Spotify playlist, but my personal Spotify has already been authorized previously using the standard Spotipy authorization flow. ``` if len(sys.argv) > 1: username = sys.argv[1] else: print('Usage: %s {myusername}' % (sys.argv[0],)) sys.exit() ```
Author
Owner

@KoenigMaurice commented on GitHub (Jul 16, 2020):

I have found a working solution!

The problem is that spotipy tries to create a file named".cache-USERNAME".
The Spotify Username looks like this: "spotify:user:ID". In windows it is not possible to create files with ":" in it.
Just remove the "spotify:user:". The ID is enough and it works fine!

<!-- gh-comment-id:659533834 --> @KoenigMaurice commented on GitHub (Jul 16, 2020): **I have found a working solution!** The problem is that spotipy tries to create a file named".cache-USERNAME". The Spotify Username looks like this: "spotify:user:ID". In windows it is not possible to create files with ":" in it. Just remove the "spotify:user:". The ID is enough and it works fine!
Author
Owner

@rlabbe commented on GitHub (Apr 8, 2021):

I think there can be several reasons for not being able to write to this file; letting pythonw through Defender seems terribly insecure.

As I understand it windows doesn't "like" dotfiles. Somehow or another my .cache file had the Hidden attribute set. I'm pretty sure because I'm using git (ya, that cache file shouldn't be put in the git repository!) When I changed its attribute the program then worked. (https://stackoverflow.com/questions/2118606/why-do-i-get-a-windows-file-permission-error-io-error-13-with-android-sdk-when)

There are ways to work around this by the user; but I suggest that spotify should recognize it is working on Windows and set attributes appropriately.

The workaround (for me) was to initialize the Auth manager with a non-dotfile cache name:

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, cache_path='cache.txt'))

I was able to change the hidden attribute of the .cache file and have it run, but seems clear this will be a problem soon enough in the future.

When looking at the code for CacheFileHandler I saw some unpythonic and potentially erroneous code. It uses the form:

 f = open()
 f.write()
 f.close()

If you get an exception on the read/write the file handle will never be closed. The following is preferred as it ensures the file handle is closed even if there is an exception:

with open() as f:
    f.write()
<!-- gh-comment-id:816043565 --> @rlabbe commented on GitHub (Apr 8, 2021): I think there can be several reasons for not being able to write to this file; letting pythonw through Defender seems terribly insecure. As I understand it windows doesn't "like" dotfiles. Somehow or another my .cache file had the Hidden attribute set. I'm pretty sure because I'm using git (ya, that cache file shouldn't be put in the git repository!) When I changed its attribute the program then worked. (https://stackoverflow.com/questions/2118606/why-do-i-get-a-windows-file-permission-error-io-error-13-with-android-sdk-when) There are ways to work around this by the user; but I suggest that spotify should recognize it is working on Windows and set attributes appropriately. The workaround (for me) was to initialize the Auth manager with a non-dotfile cache name: sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, cache_path='cache.txt')) I was able to change the hidden attribute of the .cache file and have it run, but seems clear this will be a problem soon enough in the future. When looking at the code for CacheFileHandler I saw some unpythonic and potentially erroneous code. It uses the form: f = open() f.write() f.close() If you get an exception on the read/write the file handle will never be closed. The following is preferred as it ensures the file handle is closed even if there is an exception: with open() as f: f.write()
Author
Owner

@stephanebruckert commented on GitHub (Apr 8, 2021):

As I understand it windows doesn't "like" dotfiles. Somehow or another my .cache marked as Hidden

This is interesting. If you all Windows users think we should rename these cache files to not start with a dot, let's do it! It might be a backward-incompatible change, but we are preparing a V3 for this https://github.com/plamere/spotipy/issues/652

<!-- gh-comment-id:816058639 --> @stephanebruckert commented on GitHub (Apr 8, 2021): > As I understand it windows doesn't "like" dotfiles. Somehow or another my .cache marked as Hidden This is interesting. If you all Windows users think we should rename these cache files to not start with a dot, let's do it! It might be a backward-incompatible change, but we are preparing a V3 for this https://github.com/plamere/spotipy/issues/652
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#213
No description provided.