【问题标题】:PyDrive: Invalid client secrets filePyDrive:无效的客户端机密文件
【发布时间】:2015-01-28 04:02:43
【问题描述】:

我正在尝试使用 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"

【问题讨论】:

    标签: python python-2.7 pydrive


    【解决方案1】:

    根据错误日志,您的程序找不到文件:'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,然后点击Enable API

      在 API Manager 中,单击左侧面板上的凭据。选择Add Credentials,选择OAuth 2.0 client ID,然后选择Web Application您可能需要配置一个同意屏幕,其中必填部分是产品名称,其余部分可以留空。

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

    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 个步骤。

    【讨论】:

    • 我把它放在 \Python27 和 PyDrive 目录中,也在我执行代码的同一个目录中。但仍然收到 InvalidConfigError: Invalid client secrets file File not found: "client_secrets.json"
    • 我没有看到这个,但这正是我所做的,除了我使用了 tuxdrive。
    【解决方案2】:

    首先去: https://console.developers.google.com/project

    然后转到您的项目 -> API 和身份验证 -> 凭据。 在这里你可以下载你的 client_secrets.json。

    现在将此文件 (client_secrets.json) 复制到您正在执行 .py 的同一目录中

    【讨论】:

    • 是什么让你认为 OP 没有正确地做到这一点?
    【解决方案3】:

    我遇到了同样的问题。无法登录的原因在这里:

    InvalidConfigError:无效的客户端机密文件找不到文件:“client_secrets.json”

    您需要从以下位置更改您的凭据文件名:
    client_secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com.json

    到:
    client_secrets.json

    干杯, 爸爸

    【讨论】:

    • 凭证文件名在哪里
    【解决方案4】:

    我也根据需要完成了所有步骤,但遇到了同样的错误。然后我在 PyDrive/clientsecrets.py -> _loadfile() 中添加了一个调试打印行来查看当前的工作目录。然后注意到它期望该文件与我的 Django 应用程序的 URL 模式相关。

    例如:如果这是为我的应用“文档”定义 Django URL 的方式:

    urlpatterns = 
    [
    ..
        path('testGdrive/',         views.testGdrive),
    ..
    ]
    

    例如网址:/localhost:8000/documents/testGdrive/

    如果期望文件位于“documents/testGdrive/”,而我的 JSON 文件直接位于“documents”文件夹下(“testGdrive”是虚拟文件夹)

    因此,设置完整路径解决了这个问题:

    # don't start path with '/', as this causes it to look relative to the root folder    
    client_json_path = 'documents/client_secrets.json'    
    GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = client_json_path
    

    【讨论】:

      【解决方案5】:

      确保调用文件 client_secret 而不是 client_secret.json,当你保存文件时,它实际上可能会保存为 client_secret.json.JSON,你可能会花几个小时试图弄清楚这一点。

      【讨论】:

        【解决方案6】:

        在这里也遇到了同样的问题。无论我将 client_secrets.json 放在哪里,它都不会运行(可能与我的 PyCharm 版本有关,不确定)。所以我做了一些极不推荐但救了我的事。 我进入了 PyDrive auth.py(在我的计算机中它位于此路径:/Users/MyUserName/anaconda3/lib/python3.6/site-packages/pydrive/auth.py)并在问题所在的位置更改了代码。函数LoadClientConfigFile 看起来像这样:

        if client_config_file is None:
              client_config_file = self.settings['client_config_file']
            try:
              client_type, client_info = clientsecrets.loadfile(client_config_file)
            except clientsecrets.InvalidClientSecretsError as error:
              raise InvalidConfigError('Invalid client secrets file %s' % error)
        

        所以我添加了一行,以防它不加载,只需打开它所在的文件:

        if client_config_file is None:
          client_config_file = self.settings['client_config_file']
        
        try:
          client_type, client_info = clientsecrets.loadfile(client_config_file)
        except:
          try:
            client_config_file="/Users/MyUserName/Downloads/client_secrets.json" # <--- here is where you should put your json file path
            client_type, client_info = clientsecrets.loadfile(client_config_file)
          except:
            raise InvalidConfigError('Invalid client secrets file %s' % error)
        

        【讨论】:

          【解决方案7】:

          如果您已经下载了凭证文件,将其重命名为“client_secrets.json”并将其保存在与您的脚本相同的文件夹中,但仍然出现此错误,那么尝试以下方法可能会有所帮助。

          import os 
          print (os.getcwd()) # to check the current working directory, if the current working directory above is different than your folder containing the script then change the working directory to your script directory.
          
          os.chdir('/script_directory_path') # replace '/script_directory_path' with your original script path, and place this code above 'gauth = GoogleAuth()' in your script.
          

          【讨论】:

            猜你喜欢
            • 2015-03-16
            • 2016-10-03
            • 1970-01-01
            • 2020-09-03
            • 2022-08-02
            • 2021-03-12
            • 2018-06-27
            • 2016-09-07
            • 2012-09-22
            相关资源
            最近更新 更多