【问题标题】:gapi.auth.signOut() stopped working without changes implementedgapi.auth.signOut() 在未实施更改的情况下停止工作
【发布时间】:2015-09-24 14:23:42
【问题描述】:

我使用 Google Plus 作为登录名,直到最近才正常使用。现在用户不能再注销了。回调起作用并返回用户已注销,但之后它会立即再次将用户登录。似乎它没有存储注销。

对此有一些较早的问题,例如this one。我尝试了所有建议的解决方案,但没有任何效果。

html头中的代码

<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>

按钮代码,始终显示(登录时隐藏)

<div id="signinButton">
  <span class="g-signin"
    data-scope="https://www.googleapis.com/auth/gmail.readonly"
    data-clientid="{{ CLIENT_ID }}"
    data-redirecturi="postmessage"
    data-accesstype="offline"
    data-cookiepolicy="single_host_origin"
    data-callback="signInCallback">
  </span>
</div>

登录和注销功能

<script>
  function signInCallback(authResult) {
    //console.log(authResult)
    if (authResult['code']) {

      var state = encodeURIComponent('{{ STATE }}');
      var code = encodeURIComponent(authResult['code']);          
      var tz = encodeURIComponent($('#timezone').val());
      var cntry = encodeURIComponent($('#country').val());
      var lan = encodeURIComponent('{{ language }}');

      // Send the code to the server
      $.ajax({
        type: 'POST',
        url: '/signup/gauth',
        contentType: 'application/octet-stream; charset=utf-8',
        success: function(result) {
          console.log(result)
          if (result == 'Success') {
            {% if not user %}window.location = "/user/home";{% else %}
            console.log('Logged in');{% endif %}
          }
        },
        processData: false,
        data: 'code='+code+'&state='+state+'&country='+cntry+'&tz='+tz+'&language='+lan
  });
    }
    else if (authResult['error']) {
      console.log('Sign-in state: ' + authResult['error']);
    }
  }

  function signOut() {
    gapi.auth.signOut();
    window.location = "/user/logout"
  }
</script>

【问题讨论】:

  • 嘿文森特,你检查过我正在使用的这种新的和改进的方法吗?编辑了我的答案,它应该可以解决您的问题。如果您以不同的方式解决它,请发布您的解决方案,我想研究不同的方法。谁说我不是在重新发明轮子 - 方形版本?
  • 嗨,托尼,感谢您添加答案。我通过切换到后端授权来解决它,基本上摆脱了整个 javascript 实现。因此,我很遗憾无法确认您的解决方案。
  • 没关系@vincent,似乎有几个人检查了解决方案并批准了它。因为我没有得到负面评价,所以无论如何我对赏金的结果都很满意。

标签: google-api google-plus google-signin


【解决方案1】:

编辑:我使用两步方法实现完全注销和注销:首先我注销,然后我关闭当前页面并打开一个注销 php 页面,该页面终止当前会话(我可以使用 Ajax,但我更喜欢将用户发送到注销后的主页,那何必呢?)。

<head>
  <meta name="google-signin-scope" content="profile email">
  <meta name="google-signin-client_id" content="your-CLIEntID">
  <script src="https://apis.google.com/js/platform.js" async defer>   
  </script>
  <script> 
    function signOut() {
      var auth2 = gapi.auth2.getAuthInstance();
      auth2.signOut().then(function () {
        console.log('User signed out.');
      });
    }
  </script>
</head>
<body>
...
  <a href='logout.php' onclick='signOut();'>LOGOUT</a>
...

这是我的signOut(生产),对于最终版本,我建议您删除控制台日志记录

var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut();

注销.php

<?php
ob_start();
session_start(); 
$serverName = $_SERVER['SERVER_NAME'];
$tokenGoogle = $_POST['myTokenFromGoogle'];
if ($tokenId) {
    debug_to_console("inside LOGOUT has tokenGoogle [$tokenGoogle]");
    $url = "https://accounts.google.com/o/oauth2/revoke?token=$tokenGoogle";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $json = json_decode($response, true);
    curl_close($ch);
    header("Location: http://$serverName/pageThatConsumesdata.php?".implode("|",$json));
} else {
    debug_to_console("inside LOGOUT HAVING NO tokenGoogle");
    if(isset($PHPSESSID)) {
        $message = "time for a  change of ID? ($PHPSESSID).";
        $sessionName = session_id();
        session_regenerate_id();
        $sessionName2 = session_id();
    } else {
        $message = "There was no session to destroy!";
    }
    debug_to_console($message);
    debug_to_console("[$serverName]");
    session_destroy();   
    header("Location: http://".$serverName);
    exit;
}
function debug_to_console( $data ) {
    if ( is_array( $data ) ) {
    $output = "<script>console.log( '" . implode( ',', $data) . "' );</script>";
    } else {
    $output = "<script>console.log( '" . $data . "' );</script>";
    }
    echo $output;
}
?>

注意:出于调试目的,我包含了一个用于从 php 打印到控制台的功能(单击 SHIFT+CTRL+J 在 firefox 或 chrome 中查看控制台)。这样做绝不是标准做法,应在启动最终代码后将其删除。

【讨论】:

  • 如果我这样做,我会收到“未捕获的类型错误:无法读取未定义的属性 'getAuthInstance'”。
  • 您是否更改了脚本库?我编辑并发布了一个有效的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
  • 2016-06-20
  • 2023-02-07
相关资源
最近更新 更多