简体   繁体   English

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

[英]PyDrive: Invalid client secrets file

I am trying to use PyDrive to get a list of all files in my Google Drive.我正在尝试使用 PyDrive 获取我的 Google 云端硬盘中所有文件的列表。 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.我保存了 client secrets.json,但我继续收到以下错误。 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"

Based on the error log, your program cannot find the file: 'client_secrets.json'.根据错误日志,您的程序找不到文件:'client_secrets.json'。 This file is essential as it helps identify your program to the Google API.此文件是必不可少的,因为它有助于向 Google API 识别您的程序。

Steps to get the authentication going:进行身份验证的步骤:

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

    Steps explained at: https://pythonhosted.org/PyDrive/quickstart.html步骤说明: 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:我正在复制和更新原始页面的说明,以防将来该站点不可用:

    Instructions for getting Google Drive API access获取 Google Drive API 访问权限的说明

    Go to Google Developers Console - https://console.developers.google.com and create a new project转到 Google Developers Console - https://console.developers.google.com并创建一个新项目

    Click on Enable and manage APIs , click on Drive API , then click on Enable API .单击启用和管理 API ,单击驱动 API ,然后单击启用 API

    In API Manager, click on Credentials on the left panel.在 API Manager 中,单击左侧面板上的 Credentials。 Select Add Credentials , choose OAuth 2.0 client ID , then Web Application .选择添加凭据,选择OAuth 2.0 客户端 ID ,然后选择Web 应用程序 You may need to configure a consent screen, where the required part is the Product name, and the rest you can leave blank.您可能需要配置一个同意屏幕,其中必填部分是产品名称,其余部分可以留空。

    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.在创建客户端 ID 窗口中,选择 Web 应用程序作为应用程序类型,为您的应用程序指定名称,将http://localhost:8080用于 Javascript 源,将http://localhost:8080/用于重定向 URI。 IMPORTANT: One of these ends with /, the other does not.重要提示:其中一个以 / 结尾,另一个不以 / 结尾。

  2. Download the client_secrets.json file from Google Developers Console从 Google Developers Console 下载 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 .转到 Google Developers Console - https://console.developers.google.com并找到Use Google API部分,然后单击Enable and manage APIs Select Credentials on the left panel.在左侧面板上选择凭据 You should see a list of your OAuth 2.0 client IDs.您应该会看到 OAuth 2.0 客户端 ID 的列表。 Check off the one you've created in step 1, and click on the download JSON button(looks like an arrow down icon).勾选您在第 1 步中创建的那个,然后单击下载 JSON 按钮(看起来像一个向下箭头图标)。 Rename the downloaded file to client_secrets.json.将下载的文件重命名为 client_secrets.json。

  3. Place the client_secrets.json into the project directory将 client_secrets.json 放入项目目录

    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()最好将下载的 client_secrets.json 文件放在与具有以下行的 python 程序相同的目录中:gauth.LocalWebserverAuth()

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.进行身份验证后,我建议您使用答案https://stackoverflow.com/a/24542604/820173 中的代码来保存凭据,这样您就不必每次运行代码时都进行身份验证。

For more advanced users, it is possible to create a settings.yaml file with advanced credential savings techniques.对于更高级的用户,可以使用高级凭据保存技术创建 settings.yaml 文件。 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 项目的测试文件中描述的示例:https ://github.com/googledrive/PyDrive/tree/master/pydrive/test我想提一下,这些高级的东西不是让事情顺利进行所必需的,你只需要是此答案中解释的 3 个步骤。

First go to: https://console.developers.google.com/project先去: https : //console.developers.google.com/project

then go your project -> Apis and authentication -> credentials.然后转到您的项目-> API 和身份验证-> 凭据。 Here you can download your client_secrets.json.在这里您可以下载您的 client_secrets.json。

Now copy this file (client_secrets.json) in the same directory that you are executing your .py现在将此文件 (client_secrets.json) 复制到您正在执行 .py 的同一目录中

I had the same problem.我有同样的问题。 The reason why you cannot log in is here:无法登录的原因在这里:

InvalidConfigError: Invalid client secrets file File not found : "client_secrets.json" InvalidConfigError:无效的客户端机密文件文件未找到:“client_secrets.json”

You need to change your credential file name from:您需要从以下位置更改您的凭证文件名:
client_secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com.json client_secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com.json

to:到:
client_secrets.json client_secrets.json

Cheers, Daddy干杯,爸爸

I too had all steps done as needed but was getting the same error.我也根据需要完成了所有步骤,但遇到了同样的错误。 Then I added a debug print line inside PyDrive/clientsecrets.py -> _loadfile() to see the current working directory.然后我在 PyDrive/clientsecrets.py -> _loadfile() 中添加了一个调试打印行来查看当前的工作目录。 Then noticed it was expecting the file relative to my Django app's URL pattern.然后注意到它正在等待与我的 Django 应用程序的 URL 模式相关的文件。

Eg: if this is how my Django URLs were defined for my app 'documents':例如:如果这是为我的应用程序“文档”定义 Django URL 的方式:

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

eg URL: /localhost:8000/documents/testGdrive/例如 URL:/localhost:8000/documents/testGdrive/

If was expecting the file at 'documents/testGdrive/', whereas my JSON file was directly under the 'documents' folder('testGdrive' was a virtual folder)如果期望文件位于“documents/testGdrive/”,而我的 JSON 文件直接位于“documents”文件夹下(“testGdrive”是虚拟文件夹)

Therefore, setting the full path solved this:因此,设置完整路径解决了这个问题:

# 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

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

Just had this very same problem here.刚刚在这里遇到了同样的问题。 No matter where I'd put the client_secrets.json it wouldn't run (maybe something related to my PyCharm version, not sure).无论我将client_secrets.json放在哪里,它都不会运行(可能与我的 PyCharm 版本有关,不确定)。 So I've done something highly not recommended but was what saved me.所以我做了一些非常不推荐但拯救我的事情。 I got into PyDrive auth.py (in my computer it was located in this path: /Users/MyUserName/anaconda3/lib/python3.6/site-packages/pydrive/auth.py ) and changed the code right where the problem was.我进入了 PyDrive auth.py(在我的电脑中它位于这个路径中: /Users/MyUserName/anaconda3/lib/python3.6/site-packages/pydrive/auth.py )并在问题所在的地方更改了代码. Function LoadClientConfigFile looked like this:函数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)

So I added a line to, in case it doesn't load, just open the file where it is located:所以我添加了一行,以防它没有加载,只需打开它所在的文件:

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)

If you have already downloaded the credential file, renamed it to "client_secrets.json" and have kept it in the same folder as your script, and still getting this error then it might be helpful to try the below.如果您已经下载了凭证文件,将其重命名为“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.

You can check if you named it correctly forexample for me it was client_secret.json instead of client_secrets.json您可以检查您是否正确命名它例如对我来说它是client_secret.json而不是client_secrets.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM