自动化pydrive验证过程 [英] Automating pydrive verification process
问题描述
我试图在使用 pydrive
库( GoogleAuth //pypi.python.org/pypi/PyDriverel =nofollow noreferrer> https://pypi.python.org/pypi/PyDrive )。
I am trying to automate the GoogleAuth
process when using the pydrive
library (https://pypi.python.org/pypi/PyDrive).
我设置了pydrive和google API,使我的 secret_client.json
工作,但它需要Web认证gdrive访问每次我运行我的脚本: / p>
I've set up the pydrive and the google API such that my secret_client.json
works but it requires web authentication for gdrive access every time i run my script:
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
上传到我的gdrive生成 secret_client.json
,但帐户在我授权身份验证时登录
eng.txt
is just a textfile. Moreover 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
在上一篇文章中,我尝试了以下操作来自动执行验证过程,但它提供了错误消息:
From the previous post, I've tried the following to automate the verification process but it's giving error messages:
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上的身份验证如下:
My authentication on gdrive api looks like this:
我如何使用pydrive,这样我不需要每次我使用它的身份验证?
如何允许自动认证,使python脚本使用pydrive脚本只会上传到生成 secret_client.json
的帐户,而不是上传到互联网浏览器上的当前登录帐户?
How to allow automatic authentication such that the python script using the pydrive script will only upload to the account that generated the secret_client.json
and not the currently logged on account on the internet browser?
推荐答案
首先,你误解了一个非常重要的部分如何工作:
First, you're misunderstanding one very important bit of how this works:
当我尝试使用上述脚本,而我登录到另一个
帐户。它不会上传eng.txt到我的gdrive生成
secret_client.json,但是当I
授权认证时登录的帐户
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请求数量,因此需要应用程序对其进行身份验证。
This is exactly how it's supposed to work. You, as the developer, distribute client_secret.json
with your application, and that file is used by PyDrive to authenticate the application with Google. Google wants to know how many API requests are being made by each application out there for all sorts of reasons (metrics, charge the account, revoke access, etc.), so it requires the application to authenticate itself.
现在,当您的应用程序运行 LocalWebserverAuth
时,它正在使用Google验证客户端。当然,客户端是实际使用您的应用程序的人。在这种情况下,开发人员和客户是同一个人(你),但想象你想要分发你的应用程序给一百万不同的人。他们需要能够进行身份验证,并将文件上传到自己的云端硬盘帐户,而不是让所有这些文件全部进入您的开发人员,并提供 client_secret.json
。
Now, when your application runs LocalWebserverAuth
, it's authenticating the client with Google. The client, of course, is the person actually using your application. In this case, the developer and client are the same person (you), but imagine your want to distribute your application to a million different people. They need to be able to authenticate themselves and upload files to their own Drive account, rather that having them all end up in yours (the developer), who provided client_secret.json
.
也就是说,这只是一个非常小的更改,使您的应用程序不必要求客户端每次运行应用程序时进行身份验证。您只需要使用 LoadCredentialsFile
和 SaveCredentialsFile
。 / p>
That said, it's really just a very minor change to make it so your app doesn't have to ask the client to authenticate every time you run the app. You just need to use LoadCredentialsFile
and 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')
这篇关于自动化pydrive验证过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!