Gspread - authentication error question - no AppData folder

Viewed 1860

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!

5 Answers

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.

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

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.

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).

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'

Related