我在DrEdit示例應用程序中介紹的認證過程中遇到了重定向概念問題。 這裏REDIRECT_URL由來自請求URL剝離所有參數設置:澄清Python中的OAuth流Google Drive示例應用程序(DrEdit)
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
當應用程序從谷歌驅動器UI(GET請求應用程序的根URL以get參數code
和state
)稱爲應用程序會檢查其是否有權向Google雲端硬盤發出請求。在接入已被撤銷的情況下,它會嘗試重新使用下面的代碼授權本身,我相信:
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信息中心對應用程序的訪問權限,並嘗試通過Google Drive UI將其打開,它將我重定向到授權頁面,然後在授權後重定向迴應用程序,但設法保留了狀態(從Drive UI)。我認爲這與代碼描述的內容不一致,我想知道是否有任何這種行爲的解釋。 DrEdit應用程序的託管版本可以在這裏找到:http://idning-gdrive-test.appspot.com/