2

I've actually found a work around for this issue, but I'm still stumped as to why it's happening. I just starting using gspread so I'm quite new.

After installing via pip and downloading the credentials for the Google Sheets and Drive API, when I run this code (from the gspread github), I get an error

import gspread

gc = gspread.oauth()

sh = gc.open("Example spreadsheet")

print(sh.sheet1.get('A1'))

The error:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Tyler\\AppData\\Roaming\\gspread\\credentials.json'

So obviously, the file is not in the correct AppData folder. I've looked and there is no \gspread folder in AppData. I even tried creating one to make the path correct, but it still didn't work. I eventually got it to work by using a different command and specifying the exact path of where I put the credentials file.

gc = gspread.service_account(filename='filePath')

But I'm still stumped as to why the original way did not work. I'm sure I'm missing something incredibly simple, and any help will be greatly appreciated!

tmkeeley
  • 21
  • 1
  • 3
  • gspread author here. Where did you put the credentials file initially? Could you please run this command in your command prompt: `echo %APPDATA%`? – Burnash Aug 13 '20 at 08:23
  • Hi @Burnash! Thanks for the response. I initially put the file in a general downloads folder because I couldn't find the path specified in the instructions on the gspread github. I did echo %PATH% and it shows all of my environment variables, but the app data folder is not included there. Should I add the app data folder as an environment variable? I'm quite new to programming so I'm still figuring out the basics here, sorry! – tmkeeley Aug 13 '20 at 12:21

5 Answers5

2

Had the same problem today. Was not able to find the "%APPDATA%\gspread\credentials.json" path.

So, as the guy above answered, I've created the path and just copied my credentials file in there. First try it did not work, but then I renamed the file to the same as in the instructions, "credentials.json" (before it was named "client_secret.json"). Here is the link: https://gspread.readthedocs.io/en/latest/oauth2.html

So, after that it worked just fine.

The only problem is when you get in your google acount and removes the app access from it, the code will not do the authentication again. It wll just show the following error:

google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', '{\n  "error": "invalid_grant",\n  "error_description": "Token has been expired or revoked."\n}')

So, to do the authentication again, I went to the same folder I've created and erase the "authorized_user.json" file. After doing it, the program just do the authentication again and creates a new "authorized_user.json".

Well, I think that should be a way to redo the authentication without the need to delete the "authorized_user.json" file.

Anyway, hope that this help you. The second part is jusr in case you broke because of curiosity like I did.

Have a nice day.

Snufupugos
  • 23
  • 4
1

If you want to store the credentials file somewhere else, specify the path to credentials.json and authorized_user.json in oauth():

gc = gspread.oauth(
credentials_filename='path/to/the/credentials.json',
authorized_user_filename='path/to/the/authorized_user.json'

)

source

koksal
  • 161
  • 4
0

I had the same issue... I also added a gspread folder under my AppData foler, which didn't work.

Then I looked a little closer at the path the error message was returning... and tried putting a gspread folder under AppData\Roaming, which seems to have done the trick.

memilanuk
  • 3,522
  • 6
  • 33
  • 39
  • Thanks for that! I just tried and it still doesn't seem to work for some reason. Oh well, I guess I'll continue using my work around for now. – tmkeeley Aug 11 '20 at 23:43
0

When I searched for This PC for the gspread folder, the folder name actually had some other info including gspread version. I moved the credentials file from downloads to there and it worked (I named my file client_secret.json and it still worked).

Larissa
  • 118
  • 2
  • 10
0

Windows users keep in mind that the file name shouldn't include ".json". Windows displays "account_service" but the actual file name is "account_service.json". If you add the extension ".json" you will get this error

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\YOURUSERNAME\\AppData\\Roaming\\gspread\\service_account.json'