python - 需要让 OAuth 'flow' 在 Python 上的 Google Drive 上流动,以实现独立的 py 应用程序

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

我需要获得 OAuth2“流程”才能让 GoogDRIVE 正常工作。

以前我已经获得了成功 token ,可以正常工作,但我需要对其进行设置,这样我就不需要每次都获取 token ,只需一次就可以了。

这是我所在的位置,基于: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

最佳答案

是的,这是一个简单的错误。这是替换上面 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

干杯

关于python - 需要让 OAuth 'flow' 在 Python 上的 Google Drive 上流动,以实现独立的 py 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16810266/

相关文章:

python - 如何处理南方(django)的重构?

java - 使用 Java 为 Blogger API 验证自己的 Google 帐户

c# - 与遗留系统(Forms Auth、ADFS、AD)集成的身份服务器(OAuth2)实现

.net - 如何通过.NET中的Google Drive SDK使用刷新 token 生成访问 token ?

google-drive-api - 使用自定义域发布 Google Drive 网站

javascript - 在 python django 中通过 url 发送数组

python - 检查列表是否包含从零开始的递增元素

python - 用于多个外积的 Numpy 运算符

android - OAuth2 回调在某些设备的浏览器内启动 Activity

python - 为什么 Google API V3 不返回 child ?