【问题标题】:How can I reuse an existing verification code / token to access Google Drive?如何重复使用现有的验证码/令牌来访问 Google Drive?
【发布时间】:2013-11-21 08:02:28
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    这是一个命令行工具吗?如果是这样,请尝试以下操作,这将在第一次提示您后保留凭据:

    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()
    

    【讨论】:

      猜你喜欢
      • 2020-01-27
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2019-02-16
      相关资源
      最近更新 更多