我正在为我的GDrive文件夹创建备份脚本。我需要每次运行都向Google确认脚本可以访问GDrive。但是,第一次运行后,应保存验证码。在这篇文章中,他们提到了一种使用Web服务器的解决方案(实现了Google Drive API OAuth2.0程序(无需查找验证码)),但是我正在寻找一种无需Web服务器的简单备份脚本的解决方案。
OAuth2WebServerFlow
带有现有验证码的正确凭据的类?有没有办法跳过step1_get_authorize_url()
?还是我应该oauth2
直接为此目的使用?我的密码
flow = OAuth2WebServerFlow(self.CLIENT_ID, self.CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI, offline=True)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
print
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
这是命令行工具吗?如果是这样,请尝试以下操作,这将在首次提示您后保留凭据:
import httplib2
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run
from apiclient.discovery import build
storage = Storage("saved_user_creds.dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(flow_from_clientsecrets("client_secrets2.json", scope=["https://www.googleapis.com/auth/drive"]), storage)
http = credentials.authorize(httplib2.Http())
service = build("drive", "v2", http)
print service.files().list().execute()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句