Try*_*ard 10 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'])
Run Code Online (Sandbox Code Playgroud)
我得到的错误是,如何解决这个问题?
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"
Run Code Online (Sandbox Code Playgroud)
根据错误日志,您的程序找不到文件:'client_secrets.json'.此文件非常重要,因为它有助于向Google API标识您的程序.
获取身份验证的步骤:
通过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",然后单击" 启用API".
在API Manager中,单击左侧面板上的Credentials.选择Add Credentials,选择OAuth 2.0客户端ID,然后选择Web Application. 您可能需要配置一个同意屏幕,其中所需的部分是产品名称,其余部分可以留空.
在"创建客户端ID"窗口中,选择"Web应用程序"作为"应用程序类型",指定应用程序的名称,http://localhost:8080为Javascript源和http://localhost:8080/"重定向URI".重要提示:其中一个以/结尾,另一个则不以.
从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.
将client_secrets.json放入项目目录中
最好将下载的client_secrets.json文件放在与python程序相同的目录中,该目录具有以下行:gauth.LocalWebserverAuth()
一旦您进行了身份验证,我建议您使用答案/sf/answers/1717982311/中的代码来保存凭据,这样您就不必在每次运行代码时进行身份验证.
对于更高级的用户,可以使用高级凭据保存技术创建settings.yaml文件.PyDrive项目的测试文件中描述的示例:https://github.com/googledrive/PyDrive/tree/master/pydrive/test 我想提一下,这些高级的东西不是必要的东西,所有你需要的东西这个答案中解释了3个步骤.
小智 2
首先访问: https: //console.developers.google.com/project
然后转到您的项目 -> API 和身份验证 -> 凭据。您可以在此处下载 client_secrets.json。
现在将此文件 (client_secrets.json) 复制到您正在执行 .py 的同一目录中