【问题标题】:Clarification on OAuth Flow in Python Google Drive Sample Application (DrEdit)关于 Python Google Drive 示例应用程序 (CrEdit) 中 OAuth 流程的说明
【发布时间】:2012-07-14 15:29:04
【问题描述】:

我在理解 DrEdit 示例应用程序中介绍的身份验证过程中的重定向概念时遇到了问题。 这里的redirect_url是通过从请求url中剥离所有参数来设置的:

  def CreateOAuthFlow(self):
    """Create OAuth2.0 flow controller

    This controller can be used to perform all parts of the OAuth 2.0 dance
    including exchanging an Authorization code.

    Args:
      request: HTTP request to create OAuth2.0 flow for
    Returns:
      OAuth2.0 Flow instance suitable for performing OAuth2.0.
    """
    flow = flow_from_clientsecrets('client_secrets.json', scope='')
    # Dynamically set the redirect_uri based on the request URL. This is extremely
    # convenient for debugging to an alternative host without manually setting the
    # redirect URI.
    flow.redirect_uri = self.request.url.split('?', 1)[0].rsplit('/', 1)[0]
    return flow

当从 Google Drive UI 调用应用程序时(使用 get 参数 codestate 对应用程序的根 URL 的 get 请求),应用程序会检查它是否有权向 Google Drive 发出请求。如果访问权限被撤销,它会尝试使用以下代码重新授权自己,我相信:

creds = self.GetCodeCredentials()
    if not creds:
      return self.RedirectAuth()

其中RedirectAuth() 定义为:

  def RedirectAuth(self):
    """Redirect a handler to an authorization page.

    Used when a handler fails to fetch credentials suitable for making Drive API
    requests. The request is redirected to an OAuth 2.0 authorization approval
    page and on approval, are returned to application.

    Args:
      handler: webapp.RequestHandler to redirect.
    """
    flow = self.CreateOAuthFlow()

    # Manually add the required scopes. Since this redirect does not originate
    # from the Google Drive UI, which authomatically sets the scopes that are
    # listed in the API Console.
    flow.scope = ALL_SCOPES

    # Create the redirect URI by performing step 1 of the OAuth 2.0 web server
    # flow.
    uri = flow.step1_get_authorize_url(flow.redirect_uri)

    # Perform the redirect.
    self.redirect(uri)

我的问题是,当我从 Google Dashboard 撤消对应用程序的访问权限并尝试通过 Google Drive UI 打开它时,它会将我重定向到授权页面,然后在我授权后重定向回应用程序,但它设法保留状态(从 Drive UI 传递的获取参数)。我认为这与代码描述的内容不一致,我想知道这种行为是否有任何解释。 可以在此处找到 DrEdit 应用程序的托管版本:http://idning-gdrive-test.appspot.com/

【问题讨论】:

    标签: google-app-engine oauth-2.0 google-drive-api


    【解决方案1】:

    在从 Drive UI 启动应用程序的情况下,永远不会触及该代码路径。重定向到授权端点是直接从云端硬盘启动的。也就是说,路径是:

    驱动器 -> 身份验证 -> DrEdit

    当它到达应用程序时,用户已经做出了决定。在状态查询参数中传递状态。

    要查看您所指的实际代码路径,请再次撤消访问权限。但不要从云端硬盘开始,只需尝试直接加载应用程序即可。您可能还需要删除应用程序的 cookie。无论如何,在这种情况下,当应用程序加载时,它会检测到用户没有被授权并重定向到 auth 端点:

    DrEdit -> 身份验证 -> DrEdit

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 2012-09-09
      • 2013-05-24
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      相关资源
      最近更新 更多