Google API Signout无效

时间:2017-04-26 06:53:13

标签: google-api google-oauth google-login

我集成了谷歌登录API并且它工作正常但是注销时出错。注销过程不起作用。点击退出链接功能后,登录用户不会退出。 这是我的代码:

<meta name="google-signin-client_id" content="here my api .apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<a href="javascript:signOut()">Sign Out</a>
<script>
function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();
     if(profile.getEmail()!="") {
      var myKeyVals = { token : googleUser.getAuthResponse().id_token }
      $.ajax({
        type: "POST",
        url: "validate.php",
        data: myKeyVals,
        dataType: "text",
        success : function(data) {
            window.location = "page";
        }
      });
    }
  }

  function signOut() {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut().then(function () {
    });
  }
</script>

此处signOut js功能无效,页面将自动重新加载并转到adminpage。 This is my document.

提前致谢。

4 个答案:

答案 0 :(得分:7)

这适用于我,而不是http://localhost/application-name/logoutUser您可以添加您的域名

&#13;
&#13;
document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://localhost/application-name/logoutUser";
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这对我有用。完整的JS功能代码。 星号仅是在注销后如何定义要重定向到的URL的示例。

<button onclick="myFunction()">Sign Out</button>

    <script>
    function myFunction() {
    location.href = 'https://accounts.google.com/Logout?&continue=http://******.com/';
    }
    </script>

将其保存在HTML文件中,然后从登录到要注销的帐户的浏览器中进行访问。也可以放置在PHP文件ETC中。只要您不以破坏现有脚本的方式放置它即可。

答案 2 :(得分:0)

这对我有用

简单添加一个按钮,其功能调用如下

<button onclick="myFunction()">Sign Out</button>

<script>
function myFunction() {
    gapi.auth2.getAuthInstance().signOut().then(function () {
      console.log('User signed out.');
      location.reload();
    });

}
</script>

答案 3 :(得分:0)

也许这会有所帮助。

const onGoogleSignOut = () => {
    // using google logout redirect
    //location.href = 'document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=' + window.location.href;
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut()
        .then(function () {
            auth2.disconnect();
            console.log('User signed out.');
            //location.reload();
        });
}