PyDrive:无效的客户端机密文件 [英] PyDrive: Invalid client secrets file

查看:148
本文介绍了PyDrive:无效的客户端机密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use PyDrive to get a list of all files in my Google Drive. I have read through the docs and completed all steps. I have client secrets.json saved down, but I continue to get the following error. The code I am using is:

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'])

我得到的错误是,我该如何解决?

The error I am getting is, how to do I fix this?

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识别您的程序.

Based on the error log, your program cannot find the file: 'client_secrets.json'. This file is essential as it helps identify your program to the Google API.

进行身份验证的步骤:

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

在以下位置解释了步骤: https://pythonhosted.org/PyDrive/quickstart.html

Steps explained at: https://pythonhosted.org/PyDrive/quickstart.html

我正在复制和更新原始页面上的说明,以防将来无法使用该网站:

I am copying and updating the instructions from the original page in case the site is made unavailable in the future:

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

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

Go to Google Developers Console - https://console.developers.google.com and create a new project

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

Click on Enable and manage APIs, click on Drive API, then click on Enable API.

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

In API Manager, click on Credentials on the left panel. Select Add Credentials, choose OAuth 2.0 client ID, then Web Application. You may need to configure a consent screen, where the required part is the Product name, and the rest you can leave blank.

在创建客户端ID"窗口中,将Web应用程序选择为应用程序类型",为应用程序指定名称,为Javascript起源输入http://localhost:8080,为重定向URI输入http://localhost:8080/.重要说明:其中一个以/结尾,另一个不以/结尾.

In the Create client ID window, with Web application selected as Application type, specify the Name for your application, put http://localhost:8080 for Javascript origins and http://localhost:8080/ for redirect URIs. IMPORTANT: One of these ends with /, the other does not.

从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.

Go to Google Developers Console -https://console.developers.google.com and find the Use Google API section and click on Enable and manage APIs. Select Credentials on the left panel. You should see a list of your OAuth 2.0 client IDs. Check off the one you've created in step 1, and click on the download JSON button(looks like an arrow down icon). Rename the downloaded file to client_secrets.json.

将client_secrets.json放入项目目录

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

It is best to place the downloaded client_secrets.json file in the same directory as your python program that has the following line: gauth.LocalWebserverAuth()

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

Once you got the authentication going, I would recommend you use the code from the answer https://stackoverflow.com/a/24542604/820173 to save the credentials so that you don't have to authenticate every time you run your code.

对于更高级的用户,可以使用高级凭据保存技术创建settings.yaml文件. PyDrive项目的测试文件中描述的示例: https://github.com/googledrive/PyDrive/tree/master/pydrive/test 我想提一下,这些高级知识不是使事情顺利进行的必要条件,您只需要在此答案中说明的3个步骤即可.

For more advanced users, it is possible to create a settings.yaml file with advanced credential savings techniques. Examples described in the test files for the PyDrive project: https://github.com/googledrive/PyDrive/tree/master/pydrive/test I would like to mention that this advanced stuff is not necessary to get things going, all you need are the 3 steps explained in this answer.

这篇关于PyDrive:无效的客户端机密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆