简体   繁体   English

PyDrive和Google Drive - 自动化验证流程?

[英]PyDrive and Google Drive - automate verification process?

I'm trying to use PyDrive to upload files to Google Drive using a local Python script which I want to automate so it can run every day via a cron job. 我正在尝试使用PyDrive使用本地Python脚本将文件上传到Google云端硬盘,我希望它能自动运行,因此它可以通过cron作业每天运行。 I've stored the client OAuth ID and secret for the Google Drive app in a settings.yaml file locally, which PyDrive picks up to use for authentication. 我已在本地的settings.yaml文件中存储了Google云端硬盘应用的客户端OAuth ID和密码,PyDrive会将其用于身份验证。

The problem I'm getting is that although this works some of the time, every so often it decides it needs me to provide a verification code (if I use CommandLineAuth), or it takes me to a browser to enter the Google account password (LocalWebserverAuth), so I can't automate the process properly. 我得到的问题是虽然这在某些时候有用,但每隔一段时间它决定它需要我提供一个验证码(如果我使用CommandLineAuth),或者它需要我到浏览器输入Google帐户密码( LocalWebserverAuth),所以我不能正确地自动化这个过程。

Anybody know which settings I need to tweak - either in PyDrive or on the Google OAuth side - in order to set this up once and then trust it to run automatically without further user input in future? 任何人都知道我需要调整哪些设置 - 无论是在PyDrive还是在Google OAuth端 - 为了设置一次,然后相信它会在没有用户输入的情况下自动运行?

Here's what the settings.yaml file looks like: 这是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

You can (should) create a service account - with an id and private key from the google API console - this won't require re verification but you'll need to keep the private key private. 您可以(应该)创建一个服务帐户 - 使用来自Google API控制台的ID和私钥 - 这不需要重新验证,但您需要保密私钥。

Create a credential object based on the google python example and assign it to the PyDrive GoogleAuth() object: 基于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)

EDIT (Sep 2016): For the latest integrated google-api-python-client (1.5.3) you would use the following code, with id and key the same as before: 编辑(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM