免责声明:
我不是安全专家,也不知道我可怕的问题解决方案所涉及的风险......但它会让你的代码成为 Cron 可运行的。
如果您愿意在一定范围内对您的应用进行一次手动访问接受,如谷歌快速入门示例(有用部分发布在答案底部):https ://developers.google.com/drive/web /快速入门/蟒蛇
然后,您可以将您生成CLIENT_SECRET_FILE
的文件和文件都添加到您的存储库中。drive-quickstart.json
然后,如果您有一些谷歌驱动器帐户,您总是希望您的代码库的任何副本都可以访问它,那么永远不会再检查凭据。
再次免责声明:
直观地说,这个解决方案的安全性很糟糕,一旦你的代码库以任何方式公开,那么你的谷歌驱动器就完全有被破坏、删除、禁止等的风险......
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
def get_credentials():
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive'
#ASSUME THAT THE CLIENT SECRETE FILE BELOW IS STORED IN YOUR CODE BASE
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Quickstart'
#ASSUME THAT THIS FILE BELOW IS STORED INSIDE YOUR CODE BASE
credential_dir = os.path.realpath('')
credential_path = os.path.join(credential_dir,
'drive-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatability with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def oneTimeRunCredentials():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
results = service.files().list(maxResults=10).execute()