python - 如何重复使用现有的验证码/ token 来访问 Google 云端硬盘?

标签 python google-api google-api-python-client

我正在为我的 GDrive 文件夹创建一个备份脚本。每次运行我都需要向谷歌确认 该脚本可以访问 GDrive。但是,在第一次运行后验证码应该被保存。在这篇文章中,他们提到了一个带有网络服务器的解决方案(Fulfilling Google Drive API OAuth2.0 Procedure w/o needing to find a verification code)——但我正在寻找一个没有网络服务器的简单备份脚本的解决方案。

  • 是否可以使用一个类来代替 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()

关于python - 如何重复使用现有的验证码/ token 来访问 Google 云端硬盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20074224/

相关文章:

python - 无法将 Beautiful Soup 安装到 Python 2,因为我已经将它安装在 Python 3 中,但我也无法导入或卸载它

python - 从列表中提取满足条件的随机值? Python

android - 谷歌位置 API : request location updates with pending intent?

python - Gmail API ID 无效,但线程 ID 有效

python - 如何使用 google-api-python-client 设置 BigQuery 配置属性?

python - OpenSSL.crypto.Error : [] in SignedJwtAssertionCredentials attempt

python - Scrapy:将参数传递给 cmdline.execute()

python - 我可以解压变量以检查它们在另一个列表中的成员身份吗?

google-api - GA4 数据 API 何时会进入测试版?

python - 我可以使用 Google Cloud Storage 对象作为 AutoML Vision 请求的负载吗?