PyDrive和谷歌驱动器 - 自动验证过程? [英] PyDrive and Google Drive - automate verification process?

查看:473
本文介绍了PyDrive和谷歌驱动器 - 自动验证过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PyDrive将文件上传到Google Drive,并使用本地Python脚本进行自动化处理,以便每天通过cron作业运行。我在本地将settings.yaml文件中的Google Drive应用的客户端OAuth ID和秘密存储在PyDrive中,该文件用于认证。

虽然这在某些时候有效,但每隔一段时间它就会决定需要我提供验证码(如果我使用CommandLineAuth),或者需要我到浏览器输入Google帐户密码(LocalWebserverAuth),所以我无法正确自动执行该流程。



任何人都知道我需要调整哪些设置 - 无论是在PyDrive还是在Google OAuth方面 - 为了设置它一次,然后相信它将来会自动运行,而不会在将来进一步用户输入?



以下是settings.yaml文件的样子:

  client_config_backend:设置
client_config:
CLIENT_ID:MY_CLIENT_ID
client_secret:MY_CLIENT_SECRET

save_credentials:真
save_credentials_backend:文件
save_credentials_fil e:credentials.json

get_refresh_token:False

oauth_scope:
- https://www.googleapis.com/auth/drive.file


解决方案

您可以(应该)创建一个服务帐户 - 关键是从谷歌API控制台 - 这将不需要重新验证,但你需要保持私人密钥。



创建一个凭据对象基于谷歌蟒蛇示例并将其分配给PyDrive GoogleAuth()对象:

 从从pydrive.auth进口GoogleAuth oauth2client.client进口SignedJwtAssertionCredentials 
apiclient.discovery进口构建

from pydrive.drive从Google API控制台导入GoogleDrive

# - 将私钥转换为base64或从文件加载
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 =凭证

drive = GoogleDrive(gauth)
对于最新的集成google-api-python-client(1.5.3),你可以使用下面的代码,id和key与之前一样:

 从适合的导入发现导入StringIO 
从oauth2client获得
.service_account进口ServiceAccountCredentials

凭据= ServiceAccountCredentials.from_p12_keyfile_buffer(ID,StringIO.StringIO(键),范围=的 'https://www.googleapis.com/auth/drive')
HTTP = credentials.authorize(httplib2.Http())
drive = discovery.build(drive,v2,http = http)


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. 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.

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.

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?

Here's what the settings.yaml file looks like:

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.

Create a credential object based on the google python example and assign it to the PyDrive GoogleAuth() object:

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:

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)

这篇关于PyDrive和谷歌驱动器 - 自动验证过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆