Google app engine Python Google Drive示例应用程序(DrEdit)中OAuth流的说明
我在理解DrEdit示例应用程序中演示的身份验证期间重定向的概念时遇到问题。 此处,通过从请求url中剥离所有参数来设置重定向url:Google app engine Python Google Drive示例应用程序(DrEdit)中OAuth流的说明,google-app-engine,oauth-2.0,google-drive-api,Google App Engine,Oauth 2.0,Google Drive Api,我在理解DrEdit示例应用程序中演示的身份验证期间重定向的概念时遇到问题。 此处,通过从请求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 Authoriza
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参数code
和state
向应用程序的根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传递的get参数)。我认为这与代码所描述的不一致,我想知道是否有任何关于这种行为的解释。
可以在此处找到DrEdit应用程序的托管版本:如果从驱动器UI启动应用程序,则永远不会触及该代码路径。直接从驱动器启动到授权端点的重定向。换句话说,路径是: 驱动器->身份验证->数据编辑 当它进入应用程序时,用户已经做出了决定。状态在状态查询参数中传递 要查看您在操作中引用的代码路径,请再次撤消访问权限。但是不要从驱动器开始,直接加载应用程序就可以了。您可能还需要删除应用程序的Cookie。无论如何,在这种情况下,当应用程序加载时,它将检测到用户未经授权,并重定向到身份验证端点: DrEdit->auth->DrEdit 希望有帮助