使用Google Java脚本客户端库的Google +登录 [英] Google + Signin using Google java script client library

查看:320
本文介绍了使用Google Java脚本客户端库的Google +登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

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

我能够授权用户,然后我给出了一个链接,以使用此api识别用户

I am able to authorize the user ,then i gave a link to gign out the user using this api

gapi.auth.signOut();

它也在注销用户,但是当我刷新页面时,它不再要求再次登录.它是直接加载用户帐户,我想让它要求用户再次登录. 谁能告诉我该怎么做.

it is also signing out the user, but when i refresh the page then it is not asking to sign in again. it is directly loading user account, i want to make it to ask user to again login. Can any body tell me how to do it.

推荐答案

如果您在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";
}

这篇关于使用Google Java脚本客户端库的Google +登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆