【问题标题】:Fulfilling Google Drive API OAuth2.0 Procedure w/o needing to find a verification code无需验证码即可完成 Google Drive API OAuth 2.0 流程
【发布时间】:2013-07-27 09:09:38
【问题描述】:

Google Drive API 从他们的quickstart 具有以下 OAuth2.0 过程以在最后接收 drive_service:

# Copy your credentials from the APIs Console
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'

# Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'

# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Path to the file to upload
FILENAME = 'document.txt'

# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)

# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)

请注意,您将获得打印出来的变量 authorize_url。您应该使用浏览器访问它,然后确认您允许 Google Drive 访问您的信息,然后您可以获得“验证码”。有什么方法可以避免手动干预的步骤并创建一个自动执行此步骤的程序?

【问题讨论】:

    标签: python oauth google-drive-api


    【解决方案1】:

    是的,您可以使用 Web 服务器获取 OAuth 回调,无需任何用户交互。

    基本上,您将服务器设置为检索 oauth 代码并将重定向 uri 添加到 oauth 流,以便 oauth 将代码发送到给定的 uri,而不是告诉用户将代码放入文本框中。

    看看 google-api-python-client 的 tools.run_flow() 方法。 它有非常方便的本地网络服务器 oauth 流代码。

    【讨论】:

    • 谢谢,这似乎就是我要找的东西!
    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 2021-06-28
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多