You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手使用Google Drive API遇授权错误求助

解决Google Drive API的OAuth2凭据获取错误

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.json exists but is corrupted, delete it first.
  • Run python quickstart.py again. 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.json file, and your code should run without the error.

3. Verify your client configuration file

  • Make sure you've downloaded the correct credentials.json file 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. If credentials.json is 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 read token.json but 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:09:23