python - PyDrive:无效的客户端 secret 文件

标签 python python-2.7 pydrive

我正在尝试使用 PyDrive 获取我的 Google Drive 中所有文件的列表。我已通读文档并完成所有步骤。我保存了客户端 secrets.json,但我继续收到以下错误。我使用的代码是:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
# Creates local webserver and auto handles authentication

drive = GoogleDrive(gauth)


file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
    print 'title: %s, id: %s' % (file1['title'], file1['id'])

我遇到的错误是,我该如何解决这个问题?

Traceback (most recent call last):
  File "C:\Users\mydrive\Documents\Python\Google_Drive.py", line 5, in <module>
    gauth.LocalWebserverAuth()
  File "build\bdist.win-amd64\egg\pydrive\auth.py", line 67, in _decorated
    self.GetFlow()
  File "build\bdist.win-amd64\egg\pydrive\auth.py", line 345, in GetFlow
    self.LoadClientConfig()
  File "build\bdist.win-amd64\egg\pydrive\auth.py", line 294, in LoadClientConfig
    self.LoadClientConfigFile()
  File "build\bdist.win-amd64\egg\pydrive\auth.py", line 314, in LoadClientConfigFile
    raise InvalidConfigError('Invalid client secrets file %s' % error)
InvalidConfigError: Invalid client secrets file File not found: "client_secrets.json"

最佳答案

根据错误日志,您的程序找不到文件:“client_secrets.json”。此文件至关重要,因为它有助于向 Google API 识别您的程序。

进行身份验证的步骤:

  1. 通过 Google Cloud Console 请求 Google Drive API 访问权限

    步骤说明:https://pythonhosted.org/PyDrive/quickstart.html

    我正在复制并更新原始页面的说明,以防将来该网站不可用:

    获取 Google Drive API 访问权限的说明

    转到 Google Developers Console - https://console.developers.google.com并创建一个新项目

    点击启用和管理 API,点击Drive API,然后点击启用 API

    在 API 管理器中,单击左侧面板上的凭据。选择添加凭据,选择OAuth 2.0 客户端ID,然后选择Web 应用程序您可能需要配置同意屏幕,其中必填部分是产品名称,其余部分可以留空。

    在创建客户端 ID 窗口中,选择 Web 应用程序作为应用程序类型,为您的应用程序指定 名称,为 Javascript 来源输入 http://localhost:8080http://localhost:8080/ 用于重定向 URI。重要提示:其中一个以/结尾,另一个不以/结尾。

  2. 从 Google Developers Console 下载 client_secrets.json 文件

    转到 Google Developers Console - https://console.developers.google.com并找到使用 Google API 部分并点击启用和管理 API。在左侧面板中选择凭据。您应该会看到您的 OAuth 2.0 客户端 ID 列表。勾选您在第 1 步中创建的那个,然后单击下载 JSON 按钮(看起来像一个向下箭头图标)。将下载的文件重命名为 client_secrets.json。

  3. 将client_secrets.json放入项目目录

    最好将下载的 client_secrets.json 文件放在与具有以下行的 python 程序相同的目录中: gauth.LocalWebserverAuth()

完成身份验证后,我建议您使用答案 https://stackoverflow.com/a/24542604/820173 中的代码保存凭据,这样您就不必在每次运行代码时都进行身份验证。

对于更高级的用户,可以使用高级凭据保存技术创建 settings.yaml 文件。 PyDrive 项目的测试文件中描述的示例:https://github.com/googledrive/PyDrive/tree/master/pydrive/test 我想提一下,这些高级的东西并不是让事情继续下去所必需的,你所需要的只是这个答案中解释的 3 个步骤。

关于python - PyDrive:无效的客户端 secret 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28184419/

相关文章:

Python:如何对给定的百分位数进行分组?

linux - 如何编译 Open62541 教程中的示例 xml 文件?

apache - mod_wsgi-express : error: Invalid command was specified

python - Google 驱动器多线程移动文件 PYTHON

python - PyDrive:创建 Google 文档文件

python - CatBoost出现过拟合后,有办法保存训练好的模型吗?

python - lxml html.parse 返回错误读取文件无法加载外部实体

python - 使用显示的 Plotly Express 小部件保存 Jupyter Notebook

python-2.7 - 使用 Python 中的请求库测量 HTTP 响应时间。我做对了吗?

python - 使用pydrive从google share drive下载文件,文件存在,但API返回404 File not found错误