【问题标题】:Need to get OAuth 'flow' flowing for Google Drive on Python for a stand alone py app需要为独立的 py 应用程序在 Python 上为 Google Drive 提供 OAuth“流”
【发布时间】:2013-05-24 11:59:24
【问题描述】:

我需要让 GoogDRIVE 工作的 OAuth2“流程”。

之前我已经让成功令牌工作正常,但我需要设置它,所以我不需要每次都获取令牌,一次就可以了。

这是我所在的位置,基于:https://developers.google.com/api-client-library/python/guide/aaa_oauth

import httplib2
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from apiclient.discovery import build

flow = OAuth2WebServerFlow(client_id='107......539.apps.googleusercontent.com',
                       client_secret='dVvq2itsasecretbxzG',
                       scope='https://www.googleapis.com/auth/drive',
                       redirect_uri='urn:ietf:wg:oauth:2.0:oob')

#retrieve if available
storage = Storage('OAuthcredentials.txt')
credentials = storage.get()

if  credentials is None:
    #step 1
    auth_uri = flow.step1_get_authorize_url() # Redirect the user to auth_uri
    print 'Go to the following link in your browser: ' + auth_uri
    code = raw_input('Enter verification code: ').strip()
else:
    code = credentials #yeah i know this bit is wrong, but pls send HELP!

#step 2
credentials = flow.step2_exchange(code)

#authorise 
http = httplib2.Http()
http = credentials.authorize(http)
print 'authorisation completed'

#build
service = build('drive', 'v2', http=http)

#store for next time
storage.put(credentials)

它适用于独立应用程序。所以首先我要以正确的方式去做吗? 如果是这样,我如何在上面的else 中输入code 的凭据?

【问题讨论】:

  • 天哪。一个小学生的错误!我想我需要做的就是将#step2 credentials = flow.step2_exchange(code) 放在if 语句中,而不是放在它之后......待机

标签: python oauth-2.0 google-drive-api credentials


【解决方案1】:

是的,这是一个简单的错误。这是替换上面 Q 中的位的工作片段:

......
if  credentials is None:
    #step 1
    auth_uri = flow.step1_get_authorize_url() # Redirect the user to auth_uri
    print 'Go to the following link in your browser: ' + auth_uri
    code = raw_input('Enter verification code: ').strip()
    #step 2
    credentials = flow.step2_exchange(code)
else:
    print 'GDrive credentials are still current'

#authorise
http = httplib2.Http()
http = credentials.authorize(http)
print 'Authorisation successfully completed'
......etc

干杯

【讨论】:

  • 有没有办法静默完成,因为用户不必去auth_uri来获取auth_code,应用程序本身可以获取代码并且可以完成auth流程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多