Google +使用Google javascript客户端库登录

时间:2013-12-30 07:13:09

标签: javascript google-plus google-api-js-client

我正在使用

在我的应用中进行googlre登录
gapi.auth.authorize({
        client_id : clientId,
        scope : scopes,
        immediate : true
    }, handleAuthResult);

我可以授权用户,然后我提供了一个链接,使用此API来识别用户

gapi.auth.signOut();

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

2 个答案:

答案 0 :(得分: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);
  });
},

你可以see the demo code working here。如果您打开浏览器的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";
}

答案 1 :(得分:0)

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