2012-07-14 68 views
3

我在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參數codestate)稱爲應用程序會檢查其是否有權向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/

回答

3

在從Drive UI啓動應用程序的情況下,該代碼路徑永遠不會被觸摸。重定向到授權端點直接從Drive啓動。換句話說,路徑是:

驅動器 - >權威性 - > DrEdit

通過它獲取用戶已經做出了決定,該應用程序的時間。狀態在狀態查詢參數中傳遞。

要查看您所指的代碼路徑,請再次撤銷訪問。但是不要從Drive開始,只需嘗試直接加載應用。您可能也需要刪除該應用的Cookie。無論如何,在這種情況下,當應用程序加載時,它會檢測用戶沒有被授權,並重定向到身份驗證的端點:

DrEdit - >權威性 - > DrEdit

希望有所幫助。