PyDrive:无效的客户端机密文件

时间:2015-01-28 04:02:43

标签: python python-2.7 pydrive

我正在尝试使用PyDrive获取Google云端硬盘中所有文件的列表。我已经阅读了文档并完成了所有步骤。我有客户机密码.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"

7 个答案:

答案 0 :(得分:8)

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

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

    在“创建客户端ID”窗口中,选择Web应用程序作为应用程序类型,为应用程序指定名称,为{Javascript origin}添加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()

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

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

答案 1 :(得分:3)

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

然后去你的项目 - &gt; Apis和身份验证 - &gt;证书。 在这里,您可以下载client_secrets.json。

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

答案 2 :(得分:2)

我遇到了同样的问题。您无法登录的原因如下:

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

您需要更改凭据文件名:
client_secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com.json

于:
的client_secrets.json

干杯, 爸爸

答案 3 :(得分:1)

我也根据需要完成了所有步骤,但遇到了同样的错误。然后我在 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

答案 4 :(得分:0)

确保您调用文件client_secret而不是client_secret.json,当您保存文件时,它可能会被保存为client_secret.json.JSON,您可能会花几个小时试图解决这个问题。

答案 5 :(得分:0)

刚刚在这里遇到了同样的问题。无论我将 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)

答案 6 :(得分:0)

如果您已经下载了凭证文件,将其重命名为“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.