gapi.auth.signOut()在没有实现更改的情况下停止工作

时间:2015-07-07 07:52:32

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

我正在使用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>

SignIn和SignOut功能

<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>

1 个答案:

答案 0 :(得分:4)

编辑:我使用两步方法实现完全注销和注销:首先我退出然后我关闭当前页面并打开一个注销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();

logout.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中查看控制台)。这绝不是标准做法,一旦最终代码启动就应该删除。