2

我正在使用我的应用程序进行谷歌登录

gapi.auth.authorize({
        client_id : clientId,
        scope : scopes,
        immediate : true
    }, handleAuthResult);

我能够授权用户,然后我提供了一个链接以使用此 api 退出用户

gapi.auth.signOut();

它也在注销用户,但是当我刷新页面时,它不会要求再次登录。它是直接加载用户帐户,我想让它要求用户再次登录。任何机构都可以告诉我该怎么做。

4

2 回答 2

4

如果您在 localhost 上运行,则注销将不起作用。

你所做的对我来说看起来不错。也许您错过了在您的handleAuthResult方法中注销用户的检查。这可能会发生,因为即使用户未登录,也会触发回调方法。要检查这一点,您应该确保在更改用户的登录状态之前在回调中获取访问令牌。一些有助于初始化 Google+ JavaScript API 客户端的代码:

handleAuthResult: function(authResult) {
  gapi.client.load('plus','v1', function(){
    $('#authResult').html('Auth Result:<br/>');
    for (var field in authResult) {
      $('#authResult').append(' ' + field + ': ' +
          authResult[field] + '<br/>');
    }
    if (authResult['access_token']) {
      $('#authOps').show('slow');
      $('#gConnect').hide();
      helper.profile();
      helper.people();
    } else if (authResult['error']) {
      // There was an error, which means the user is not signed in.
      // As an example, you can handle by writing to the console:
      console.log('There was an error: ' + authResult['error']);
      $('#authResult').append('Logged out');
      $('#authOps').hide('slow');
      $('#gConnect').show();
    }
    console.log('authResult', authResult);
  });
},

您可以在此处查看演示代码。如果您打开浏览器的 JavaScript 控制台,您将在用户退出时注意到以下错误消息:

authResult 
...
Object {error: "user_signed_out", 
cookie_policy: "single_host_origin"
error: "user_signed_out"
expires_at: "1388530488"
expires_in: "86400"
g-oauth-window: undefined
g_user_cookie_policy: "single_host_origin"
issued_at: "1388444088"
response_type: "code token id_token gsession"
scope: "https://www.googleapis.com/auth/plus.login 
session_state: "707059cda8acedf0acf31d83c713e9f16f232610..25c4"
status: Object
google_logged_in: true
method: null
signed_in: false
...

如果用户自动/重复退出站点,很可能是由于 cookie 存储已损坏的已知问题。如果您遇到此问题,请在隐身窗口中打开目标页面或删除当前 cookie,例如在开发者控制台中运行以下 javascript:

var cookies = document.cookie.split(";");

for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i];
    var eqPos = cookie.indexOf("=");
    var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
于 2013-12-30T22:57:22.193 回答
0

我尝试使用@class 答案,它仍然无法使用signOut(),当我们调用gapi.auth.authorize 时没有错误。但我找到了另一种方法,尝试访问链接 https://accounts.google.com/o/oauth2/revoke?token=authResult['access_token'],这会让你喜欢注销,用户需要重新接受授权。虽然我现在有点困惑,我们应该使用哪种方式来 signOut();

于 2014-02-18T06:46:15.650 回答