PyDrive:无效的客户端机密fi

2024-09-21 05:48:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用PyDrive获取我的Google驱动器中所有文件的列表。我已经阅读了文档并完成了所有步骤。我已经保存了client 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"

Tags: inpybuildclientauthegglinewin
3条回答

首先转到: https://console.developers.google.com/project

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

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

根据错误日志,程序找不到文件:“client_secrets.json”。这个文件非常重要,因为它有助于在Google API中识别您的程序。

进行身份验证的步骤:

  1. 通过Google云控制台请求Google Drive API访问

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

    我正在从原始页面复制和更新说明,以防将来无法访问该网站:

    获取Google Drive API访问的说明

    转到Google开发者控制台-https://console.developers.google.com并创建一个新项目

    单击“启用和管理API”,单击“驱动API”,然后单击“启用API”。

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

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

  2. 从谷歌开发者控制台下载client_secrets.json文件

    转到Google开发人员控制台-https://console.developers.google.com,找到使用Google API部分,然后单击启用和管理API。选择左侧面板上的“凭据”。您应该会看到OAuth2.0客户机ID的列表。勾选您在步骤1中创建的那个,然后单击下载JSON按钮(看起来像一个向下箭头的图标)。将下载的文件重命名为client_secrets.json。

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

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

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

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

我也有同样的问题。无法登录的原因如下:

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

您需要将凭据文件名从:
客户端_secret_xxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com.json

收件人:
客户端_secrets.json

干杯, 爸爸

相关问题 更多 >

    热门问题