python - 自动化 pydrive 验证过程

标签 python google-api cloud google-drive-api pydrive

我正在尝试在使用 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 上的身份验证如下所示:

enter image description here

如何使用 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

也就是说,它实际上只是一个非常小的更改,因此您的应用程序不必在每次运行应用程序时都要求客户端进行身份验证。您只需要使用 LoadCredentialsFileSaveCredentialsFile .

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/

相关文章:

python - django 在 heroku 上发布 url

python - 在 Python 中将 np.arrays 附加到空白数组

python - 使用 Numpy 计算一系列元素

python - 不允许空格作为最后一个字符

javascript - 如何获得Youtube直播位置?

python - 如何使用带有 Python 的 Google Drive API 覆盖文件?

google-app-engine - 谷歌云存储性能和全文搜索

python - 如何使用 Flask 可插入 View 重用查看和编辑代码

oauth-2.0 - Google API 登录按钮 : Basic v. 自定义

php - 如何扩展数据库/CPU 密集型脚本?