【问题标题】:Signing out of Google+ using JS使用 JS 退出 Google+
【发布时间】:2015-04-17 13:29:53
【问题描述】:

我正在尝试在我的网站上放置一个 Google+ 登录按钮。该按钮完美运行。我也有一个退出按钮。我在用户登录时隐藏了登录按钮。我的代码在这里:

<span id="signinButton">
    <span
        class="g-signin"
        data-callback="signinCallback"
        data-clientid="******************"
        data-cookiepolicy="single_host_origin"
        data-requestvisibleactions="http://schema.org/AddAction"
        data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email">
    </span>
</span> &nbsp;&nbsp;&nbsp;&nbsp;
<button id="revokeButton" onclick="gapi.auth.signOut()">Sign Out</button>

<script>
    function signinCallback(authResult) {
        if (authResult['status']['signed_in']) {
            document.getElementById('signinButton').setAttribute('style', 'display: none');
            console.log("User successfully logged in!!");
        } else {
            console.log('Sign-in state: ' + authResult['error']);
        }
    }
</script>
<script>
    function disconnectUser(access_token) {
        var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' +
                access_token;

        // Perform an asynchronous GET request.
        $.ajax({
            type: 'GET',
            url: revokeUrl,
            async: false,
            contentType: "application/json",
            dataType: 'jsonp',
            success: function(nullResponse) {
                document.getElementById('signinButton').setAttribute('style', 'display: display');
                // The response is always undefined.
                console.log("Success in logging out!");
            },
            error: function(e) {
                // Handle the error
                console.log(e);
                // You could point users to manually disconnect if unsuccessful
                // https://plus.google.com/apps
            }
        });
    }
</script>

问题是当我使用 gapi.auth.signOut() 注销时...它会注销我,但刷新时再次将同一用户登录到 Google+。如何允许其他人登录我的网站。如何从 Google 完全注销人员。我是 Javascript 新手...举个例子会有所帮助。

【问题讨论】:

  • 你是在页面加载时调用登录函数吗?
  • 否..仅当单击 signinButton 时..当我单击按钮时...出现一个对话框并消失...这意味着以前的用户已登录。
  • 我的第一个问题是:您打算让您的网站成为用户登录和退出 google+ 的代理,还是打算使用 google+ 作为授权帐户创建访问的一种方式到您的网站?如果我访问 someSite.com 并使用 google+ 登录,然后退出该站点,我不会期望实际退出我的 google+ 帐户。可能是因为 google 正在执行一项规则,即第三方网站不能让用户退出 google+。
  • 嗯,刷新后一定有什么东西试图让用户登录。如果您的页面上还有 Google-API javascript,请将其添加到您的问题中。
  • 好吧,我想我的下一个问题是,如果您没有捕获和保存数据以与您网站的唯一身份访问者相关联,那么他们为什么还要登录呢?在我看来,也许是天真的,就像这样无缘无故地限制用户。是否打算向 g+ 用户授予其他方式不可用的特殊内容?

标签: javascript google-plus


【解决方案1】:

我想出的答案很简单。用户第一次登录时,他没有登录到我的网站。所以显然用户必须登录。下次页面刷新时......Google+ 检查用户是否已经登录到任何 Google 服务,如 gmail 等。如果答案为真,则让用户保持登录状态。要检查这是否有效...注销您的任何 Google 服务(gmail 等)并尝试使用 Google+ 登录。它将要求提供凭据。因此,一旦用户登录 Google+,他/她不必每次都保持登录。

【讨论】:

    【解决方案2】:

    这个答案更像是一个笑话,但你为什么不尝试通过使用src of https://accounts.google.com/Logout 向正文添加隐藏的 iframe 来从用户的谷歌帐户注销用户的 hacky 方法?

    由于 X-Frame-Options 设置为 DENY,它将拒绝显示,但实际上会将您注销。

    使用 jquery 的单行代码:

    $('<iframe style="display: none" src="https://accounts.google.com/Logout"></iframe>').appendTo(document.body);
    

    【讨论】:

    • 不建议依赖 hacky 的副作用。这可能会随时停止工作,从而使构建它的网站损坏。
    • 当然不是。提到它是一个笑话,而不是一个解决方案。 OP应该知道他所有的选择。不只是正确的;)
    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2017-08-29
    • 2023-03-20
    • 1970-01-01
    • 2022-07-07
    相关资源
    最近更新 更多