Google登录API - 如何使用PHP登录某人?

问题描述 投票:8回答:5

在Google的Integrate Google Sign-In页面上,它底部的部分向您展示了如何使用Javascript签署用户:

<a href="#" onclick="signOut();">Sign out</a>
<script>
  function signOut() {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut().then(function () {
      console.log('User signed out.');
    });
  }
</script>

我一直在寻找,我找不到使用PHP这样签署用户的方法。

我确实找到了如何完全退出Google的用户,但我不希望这样。我也知道我可以删除包含访问代码的$_SESSION变量,但这仍然不完全是我想要的。

有谁知道如何使用PHP将某人从我的Google应用程序中删除?

javascript php google-signin google-api-php-client
5个回答
4
投票

这应该工作,我用Mark Guinn的代码解决了问题,这是gapi.auth2.init();方法的任务没有完成执行的事实。 .next()'ing它解决了这个问题。

<?php 
    session_start();
    session_unset();
    session_destroy();
?>
<html>
    <head>
        <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
    </head>
    <body>
        <script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>
        <script>
            window.onLoadCallback = function(){
                gapi.load('auth2', function() {
                    gapi.auth2.init().then(function(){
                        var auth2 = gapi.auth2.getAuthInstance();
                        auth2.signOut().then(function () {
                            document.location.href = 'login.php';
                        });
                    });
                });
            };
        </script>
    </body>
</html>

0
投票

JavaScript是操作不同于您的域的cookie的唯一方法。


0
投票

检查一下这对你有用

header('Location:https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://www.domain.com');

0
投票

如果你打算在服务器端的auth2.signOut()上签一个用户,check out this code(虽然它是python,你应该明白这个想法)。

app.signOut = function() {
  // Get `GoogleAuth` instance
  var auth2 = gapi.auth2.getAuthInstance();
  // Sign-Out
  fetch('/signout', {
    method: 'POST',
    credentials: 'include'
  }).then(function(resp) {
    if (resp.status === 200) {
      auth2.signOut()
      .then(changeProfile);
    } else {
      console.error("couldn't sign out");
    }
  }).catch(function(error) {
    console.error(error);
  });
};

And this one

@app.route('/signout', methods=['POST'])
def signout():
    # Terminate sessions
    session.pop('id', None)

    return make_response('', 200)

这取决于您如何构建会话,但您可以在signOut()之前向服务器发送ajax请求。


-1
投票

为什么不让你的注销脚本看起来像这样:

<?php
session_start();
session_unset();
session_destroy();
?>
<html>
   <head>
     <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
   </head>
   <body>
     <script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>
     <script>
       window.onLoadCallback = function(){
         gapi.load('auth2', function() {
           gapi.auth2.init();
           var auth2 = gapi.auth2.getAuthInstance();
           auth2.signOut().then(function () {
             document.location.href = 'login.php';
           });
         });
       };
     </script>
   </body>
 </html>
© www.soinside.com 2019 - 2024. All rights reserved.