且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Google Java脚本客户端库的Google +登录

更新时间:2021-11-23 22:26:41

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

If you are running on localhost, signout will not work.

您正在做的事情对我来说似乎很好.也许您缺少handleAuthResult方法中正在注销的用户的支票.之所以会发生这种情况,是因为即使用户未登录,也会触发回调方法.要进行检查,您应该确保在更改用户的登录状态之前,已在回调中获取访问令牌.一些有助于初始化Google+ JavaScript API客户端的代码:

What you are doing looks like good to me. Perhaps you are missing a check for the user being signed out in your handleAuthResult method. This could be happening because the callback method is going to be triggered even when the user is not signed in. To check this, you should be ensuring that you are getting an access token in your callback before changing the sign in status for your users. Some code to help that will also initialize the Google+ JavaScript API client:

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控制台,则在用户注销后会注意到以下错误消息:

You can see the demo code working here. If you open your browser's JavaScript console, you will notice the following error message when the user is signed out:

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:

If the user is automatically / repeatedly signing out of the site, it's most likely due to a known issue where the cookie store is corrupted. If you are seeing this issue, either open the target page in an incognito window or delete the current cookies, e.g. run the following javascript in the developer console:

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";
}