- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试在使用 pydrive
库 (https://pypi.python.org/pypi/PyDrive) 时自动执行 GoogleAuth
过程。
我已经设置了 pydrive 和 google API,以便我的 secret_client.json
可以正常工作,但是每次我运行我的脚本时它都需要对 gdrive 进行 Web 身份验证:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
textfile = drive.CreateFile()
textfile.SetContentFile('eng.txt')
textfile.Upload()
print textfile
drive.CreateFile({'id':textfile['id']}).GetContentFile('eng-dl.txt')
eng.txt
只是一个文本文件。此外,当我在登录另一个帐户时尝试使用上述脚本时。 它不会将 eng.txt
上传到生成 secret_client.json
的我的 gdrive 中,而是在我授权身份验证时登录的帐户
在上一篇文章中,我尝试了以下方法来自动化验证过程,但它给出了错误消息:
import base64, httplib2
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
#gauth = GoogleAuth()
#gauth.LocalWebserverAuth()
# from google API console - convert private key to base64 or load from file
id = "464269119984-j3oh4aj7pd80mjae2sghnua3thaigugu.apps.googleusercontent.com"
key = base64.b64decode('COaV9QUlO1OdqtjMiUS6xEI8')
credentials = SignedJwtAssertionCredentials(id, key, scope='https://www.googleapis.com/auth/drive')
credentials.authorize(httplib2.Http())
gauth = GoogleAuth()
gauth.credentials = credentials
drive = GoogleDrive(gauth)
drive = GoogleDrive(gauth)
textfile = drive.CreateFile()
textfile.SetContentFile('eng.txt')
textfile.Upload()
print textfile
drive.CreateFile({'id':textfile['id']}).GetContentFile('eng-dl.txt')
错误:
Traceback (most recent call last):
File "/home/alvas/git/SeedLing/cloudwiki.py", line 29, in <module>
textfile.Upload()
File "/usr/local/lib/python2.7/dist-packages/pydrive/files.py", line 216, in Upload
self._FilesInsert(param=param)
File "/usr/local/lib/python2.7/dist-packages/pydrive/auth.py", line 53, in _decorated
self.auth.Authorize()
File "/usr/local/lib/python2.7/dist-packages/pydrive/auth.py", line 422, in Authorize
self.service = build('drive', 'v2', http=self.http)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/apiclient/discovery.py", line 192, in build
resp, content = http.request(requested_url)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 475, in new_request
self._refresh(request_orig)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 653, in _refresh
self._do_refresh_request(http_request)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 677, in _do_refresh_request
body = self._generate_refresh_request_body()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 861, in _generate_refresh_request_body
assertion = self._generate_assertion()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 977, in _generate_assertion
private_key, self.private_key_password), payload)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/crypt.py", line 131, in from_string
pkey = crypto.load_pkcs12(key, password).get_privatekey()
OpenSSL.crypto.Error: [('asn1 encoding routines', 'ASN1_get_object', 'header too long')]
我在 gdrive api 上的身份验证如下所示:
如何使用 pydrive,这样我每次使用时都不需要进行身份验证?
如何允许自动身份验证,这样使用 pydrive 脚本的 python 脚本只会上传到生成 secret_client.json
的帐户,而不是 Internet 浏览器上当前登录的帐户?
最佳答案
首先,您对它的工作原理有一个非常重要的误解:
when I try to use the above script while I am logged into another account. It doesn't upload the eng.txt into my gdrive that generated the secret_client.json but the account that was logged in when I authorize the authentication
这正是它应该如何工作的。作为开发人员,您将 client_secret.json
与您的应用程序一起分发,PyDrive 使用该文件向 Google 验证 应用程序。 Google 想知道每个应用程序出于各种原因(指标、帐户收费、撤销访问权限等)发出了多少 API 请求,因此它要求应用程序对其自身进行身份验证。
现在,当您的应用程序运行 LocalWebserverAuth
时,它正在向 Google 验证 客户端。当然,客户是实际使用您的应用程序的人。在这种情况下,开发人员和客户是同一个人(您),但想象一下您希望将您的应用程序分发给一百万不同的人。他们需要能够对自己进行身份验证并将文件上传到他们自己的云端硬盘帐户,而不是让他们最终都在您(开发人员)的帐户中,他们提供了 client_secret.json
。
也就是说,它实际上只是一个非常小的更改,因此您的应用程序不必在每次运行应用程序时都要求客户端进行身份验证。您只需要使用 LoadCredentialsFile
和 SaveCredentialsFile
.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
textfile = drive.CreateFile()
textfile.SetContentFile('eng.txt')
textfile.Upload()
print textfile
drive.CreateFile({'id':textfile['id']}).GetContentFile('eng-dl.txt')
关于python - 自动化 pydrive 验证过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24419188/
我知道如何使用 PyDrive 从我的驱动器下载文件,问题是我需要在共享驱动器上下载(或至少打开)一个 xlsx 文件。到目前为止,这是我下载文件的代码: from pydrive.auth impo
我使用库pydrive对于谷歌驱动器。现在我需要获取 Google 云端硬盘中的所有文件夹(根、父、子)名称。 我发现了这个: self.drive.ListFile().GetList() 但它返回
对不起我的英语。我使用 pydrive 与 google drive api 一起工作。我想获取文件列表。我这样做: return self.g_drive.ListFile({'q': 'tra
我的 Google 云端硬盘看起来像这样: picture1.jpg song1.mp3 a/b/file1.txt a/b/file2.txt a/b/file3.jpg a/b/file4.m4a
我一直在尝试使用 PyDrive 将文件从一个文件夹移动到我的谷歌驱动器中的另一个文件夹 file2 = drive.CreateFile({'id': }) file2['parents']= fi
我正在尝试将文件上传到我的 Google 驱动器,下面的代码有效。 如何指定要上传到哪个文件夹,即驱动器---与我共享--csvFolder from pydrive.auth import Goog
我正在尝试将 .doc 文件作为 html 从 Google 云端硬盘导出到。这是我的代码。我在文档中没有看到任何有关如何将文档下载为 html 的内容。但这里是我到目前为止的代码示例。我不确定 do
我正在尝试对 PyDrive 使用双重/口是心非。但是,我不断收到一条非常神秘的错误消息,告诉我引用 PyDrive 安装联机帮助页。没有提供解决方案。 这是我正在运行的重复命令的输出,包括错误。 $
我正在使用 PyDrive 将文件从我使用 Pyinstaller 打包的桌面 Python 应用程序上传到 GoogleDrive。我想尽可能地隐藏 client_secrets.json,所以我将
我正在使用 PyDrive ( http://pythonhosted.org/PyDrive/ ) 来简化对 Google 云端硬盘的 API 访问,但在尝试从云端硬盘中删除现有文件时遇到了障碍。我
在我的应用程序中,用户 A 使用 PyDrive 和 Google Drive 创建了一个文件,并使用保存到云端硬盘 self.User.GoogleFile.SetContentString(jso
我使用 Python 和 PyDrive 编写了一个程序。该程序捕获图像并将其上传到 Google Drive 上的文件夹。这是我的身份验证和上传的基本代码: gauth = GoogleAuth()
我正在尝试在使用 pydrive 库 (https://pypi.python.org/pypi/PyDrive) 时自动执行 GoogleAuth 过程。 我已经设置了 pydrive 和 goog
我正在尝试使用从(Ubuntu)服务器远程工作的python脚本自动将文件上传到谷歌驱动器。 在我的代码中,我有以下来自 Pydrive 的简单行: from pydrive.auth import
我正在尝试通过 PyDrive 将一个大型 csv 压缩文件(~6GB)上传到 Google Drive,但每次我尝试通过以下方式上传时: file = self.drive.CreateFile({
我正在尝试安装 PyDrive,以便将我的工作文件夹自动备份到 Google 云端硬盘。 我正在尝试使用 pip 在终端中安装 PyDrive,但没有这样的运气。 当我运行时 pip install
我想使用 Google Drive API 使用 cronjob 在其上存储一些备份。我只是不明白如何使用服务帐户使用 PyDrive。当我生成服务帐户文件时,我将它作为我的脚本放在目录中 clien
我正在尝试使用 PyDrive 从 Google Drive 下载文件。我按照指示http://pythonhosted.org/PyDrive/filemanagement.html#downloa
我正在使用 PyDrive 在 Google 云端硬盘中创建文件,但我在处理实际的 Google 文档类型项目时遇到了问题。 我的代码是: file = drive.CreateFile({'titl
我正在使用 pydrive 1.2.1 在 python 3.5.2 中编写一个小脚本,我需要能够删除一个文件,该文件不是存储在我的计算机本地,而是存储在我的 Google Drive 帐户中。 do
我是一名优秀的程序员,十分优秀!