2013-05-13 63 views
6

我目前正在嘗試創建一個網站,用戶可以使用他的Google +帳戶登錄。大部分工作。我讓他們授予訪問我的網站。他們可以登錄並獲取他們的姓名和用戶ID,並在我的網站上顯示特定於他們的Google帳戶的內容。Google Auth2.0註銷

當有人想要登錄時,我嘗試登出網站,但Google登錄仍然記得剛剛登錄,登錄後立即運行代碼以再次登錄。如果我從谷歌刪除SSID cookie,它不會這樣做,所以我假設這是谷歌存儲我剛剛用x登錄的事實。

有沒有辦法讓我註銷時,谷歌不會立即用相同的帳戶登錄,而是要求谷歌用戶的電子郵件和密碼?

我覺得我錯過了一些明顯的東西,但我無法弄清楚如何處理這個問題。

<button class ="btn btn-primary" id="authorize-button" style="visibility: hidden">Log in</button> 

<script> 

    var clientId = ''; 

    var apiKey = ''; 

    var scopes = ''; 


    function handleClientLoad() { 

    gapi.client.setApiKey(apiKey); 
    window.setTimeout(checkAuth,1); 
    } 

    function checkAuth() { 
    //alert("authorize"); 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult); 
    } 


    function handleAuthResult(authResult) { 
    //alert("authorized"); 


    //alert(authResult.access_token); 
    var authorizeButton = document.getElementById('authorize-button'); 
    if (authResult && !authResult.error) { 
     authorizeButton.style.visibility = 'hidden'; 
     makeApiCall(); 
    } else { 
     authorizeButton.style.visibility = ''; 
     authorizeButton.onclick = handleAuthClick; 
    } 
    var token = document.createElement('h4'); 
    token.appendChild(document.createTextNode(authResult.access_token)); 
    document.getElementById('content').appendChild(token); 



    } 

    function handleAuthClick(event) { 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult); 
    return false; 
    } 

    var x; 
    function makeApiCall() { 

    //return; 
    gapi.client.load('plus', 'v1', function() { 
     var request = gapi.client.plus.people.get({ 
     'userId': 'me' 
     }); 
     request.execute(function(resp) { 
     x = resp.id; 
     var heading2 = document.createElement('h4'); 
     var heading3 = document.createElement('h4'); 
     heading3.appendChild(document.createTextNode(resp.displayName)); 
     heading2.appendChild(document.createTextNode(resp.id)); 

     document.getElementById('content2').appendChild(heading2); 
     document.getElementById('content3').appendChild(heading3); 


     $.post("token.php", {id: x}); 
     }); 

    }); 

    } 
+0

這是一個關鍵問題,我覺得Google絕對不支持這一點。 – 2015-02-05 10:50:56

+0

Is gapi.auth.signOut();不是?我在Google頁面上看到它,同時尋找目標c等價物。 – 2015-02-11 15:49:37

回答

0

當你在auth電話,設置approvalprompt給力:我用它來驗證和獲取數據

代碼。這將強制每次出現同意對話框。它覆蓋「auto」的默認設置。您可以通過https://developers.google.com/+/web/signin/#sign-in_button_attributes瞭解更多信息。

gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true, approvalprompt: force}

+0

這不解決問題,也不是批准提供強制註銷解決方案。它僅在某些「登錄」情況下有用,例如,當您需要刷新正在使用的作用域時,或者如果丟失了用戶的刷新標記並需要強制生成新的標記。 – BrettJ 2013-05-14 14:58:17

+0

它可能無法解決註銷功能這個大問題 - 但它確實解決了如何提示同意對話框的明確問題。我同意這不是退出的解決方案。 – Joanna 2013-05-15 16:48:33

0

用戶授權您的應用程序後,他們基本上登錄到您的應用程序,他們也登錄到谷歌的任何時候,特別是當直接模式開啓。

有些網站所做的是有一個註銷鏈接或按鈕,顯示一個頁面或對話框,其中顯示了一些內容,如「您使用帳戶[email protected]登錄Google和此網站」。切換帳戶,轉到google.com並退出Google會話。「

您還可以使用自己的cookie跟蹤用戶的登錄狀態,並在代碼中的相應事件中設置並刪除它們。您會希望放棄您的應用在註銷事件期間代表用戶獲取的任何令牌。當用戶再次登錄時,他們不需要使用彈出窗口(或重定向窗口)重新授權您的應用程序,但在回調過程中仍然會獲得新的訪問令牌。