Python新手使用Google Drive API遇授权错误求助
Hey there! Let's work through this Google Drive API issue you're facing. That error trace you shared points to a problem with loading OAuth2 credentials—most likely the token.json file is missing, corrupted, or wasn't properly generated in the first place. Here's how to fix it step by step:
1. Check if the credentials file exists
- First, look in your project directory for a file named
token.json. This file gets created automatically when you complete the initial Google account authorization flow. - If this file is missing, it means the authorization process never finished successfully (maybe you closed the browser window early, or there was a network issue).
2. Re-run the authorization flow
- If
token.jsonexists but is corrupted, delete it first. - Run
python quickstart.pyagain. This should launch a browser window asking you to log into your Google account and grant permission for the app to access your Drive. - Once you complete the authorization, the script will generate a valid
token.jsonfile, and your code should run without the error.
3. Verify your client configuration file
- Make sure you've downloaded the correct
credentials.jsonfile from the Google Cloud Console and placed it in your project directory. - This file contains your app's client ID and secret—without it, the script can't start the authorization flow to create
token.json. Ifcredentials.jsonis missing or incorrect, the whole process breaks down.
4. Fix dependency issues (if needed)
If you're using the older oauth2client library (which your error trace shows), double-check it's installed properly:
pip install oauth2client
Note: Google now recommends using google-auth and google-auth-oauthlib for newer projects, but if you're following the classic quickstart guide, oauth2client should work fine as long as it's installed correctly.
Quick note on the error: The line
client.Credentials.new_from_json(content)failing means the script tried to readtoken.jsonbut got invalid JSON content—either the file is empty, has wrong formatting, or is missing required fields. Deleting and regenerating it is the simplest fix here.
内容的提问来源于stack exchange,提问作者Rana Ranvijay Singh

