I’m having trouble with my Discord bot that’s supposed to connect to Google Sheets API. When I try to run a slash command that should write data to a spreadsheet, I keep getting this error message saying “No key or keyfile set”.
I’ve already tried a few things to fix this problem. First, I made sure my service account has access to the Google Sheet by sharing it properly. I also double checked all the credentials in my environment file to make sure everything matches what Google gave me. I even tried putting the private key directly in my code instead of using the env file, but I’m pretty new to this so I might have done it wrong.
The weird thing is that no matter what I try, I keep getting the exact same error. I was expecting to at least get a different error message if I was making progress, but nothing seems to work. Is this definitely a problem with how I’m handling the authentication keys, or could there be something else going on with the Google API setup?
the bot can’t find ur keyfile. check if ur json file is in the right directory - it needs to be relative to where ur bot script runs. I had the same issue, was just a path problem. my keyfile ended up in a different folder than I thought.
Had this exact problem last month setting up my bot. The issue was formatting the private key from the environment variable wrong. When you copy the private key into your env file, the newline characters get messed up. You need to replace all the literal \n in your private key string with actual newlines. I did private_key = os.getenv('PRIVATE_KEY').replace('\\n', '\n') before passing it to the credentials object. Also make sure your environment variables load before you try to authenticate - I was calling the Google API before my bot finished loading the env file.
This error usually means your Google Sheets API credentials aren’t set up right. I ran into the same thing - turns out I wasn’t initializing the credentials properly. Make sure you’re using the right method: if you’ve got credentials as a dictionary, use google.oauth2.service_account.Credentials.from_service_account_info(). If you’re using a JSON file, go with from_service_account_file(). Also check that your environment variables are actually loading in the Discord bot - try logging part of the key to see if it’s getting set correctly.