[英]PyDrive and Google Drive - automate verification process?
我正在尝试使用PyDrive使用本地Python脚本将文件上传到Google云端硬盘,我希望它能自动运行,因此它可以通过cron作业每天运行。 我已在本地的settings.yaml文件中存储了Google云端硬盘应用的客户端OAuth ID和密码,PyDrive会将其用于身份验证。
我得到的问题是虽然这在某些时候有用,但每隔一段时间它决定它需要我提供一个验证码(如果我使用CommandLineAuth),或者它需要我到浏览器输入Google帐户密码( LocalWebserverAuth),所以我不能正确地自动化这个过程。
任何人都知道我需要调整哪些设置 - 无论是在PyDrive还是在Google OAuth端 - 为了设置一次,然后相信它会在没有用户输入的情况下自动运行?
这是settings.yaml文件的样子:
client_config_backend: settings
client_config:
client_id: MY_CLIENT_ID
client_secret: MY_CLIENT_SECRET
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json
get_refresh_token: False
oauth_scope:
- https://www.googleapis.com/auth/drive.file
您可以(应该)创建一个服务帐户 - 使用来自Google API控制台的ID和私钥 - 这不需要重新验证,但您需要保密私钥。
基于google python 示例创建一个凭证对象,并将其分配给PyDrive GoogleAuth()对象:
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
# from google API console - convert private key to base64 or load from file
id = "...@developer.gserviceaccount.com"
key = base64.b64decode(...)
credentials = SignedJwtAssertionCredentials(id, key, scope='https://www.googleapis.com/auth/drive')
credentials.authorize(httplib2.Http())
gauth = GoogleAuth()
gauth.credentials = credentials
drive = GoogleDrive(gauth)
编辑(2016年9月):对于最新的集成google-api-python-client(1.5.3),您将使用以下代码,id和key与以前相同:
import StringIO
from apiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials
credentials = ServiceAccountCredentials.from_p12_keyfile_buffer(id, StringIO.StringIO(key), scopes='https://www.googleapis.com/auth/drive')
http = credentials.authorize(httplib2.Http())
drive = discovery.build("drive", "v2", http=http)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.