我正在尝试解决这个问题,但我无法理解问题所在。我已经在我的网站上集成了Google登录,除了退出外,一切正常。我的网站有两个部分:
www.website.com
www.website.com/admin // admin dashboard
在这两个地方我都有这个代码: 在头部
<meta name="google-signin-client_id" content="my-client-id.apps.googleusercontent.com">
身体标签的末尾
<script src="js/scripts.js"></script>
<script src="https://apis.google.com/js/platform.js?onload=googleLoad" async defer></script>
在scripts.js文件中
$(document).ready(function(){
//logout of google before logging out of site
$('.log-out').on('click', function(e){
e.preventDefault();
console.log('log out interrupted');
var href = $(this).attr('href');
loggedOut(href);
});
});
function googleLoad() {
console.log('google load exec');
gapi.load('auth2', function() {
gapi.auth2.init();
});
};
function loggedOut(href){
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function(){
console.log('User signed out.');
document.location = href;
});
};
此部分在正面和背面完全相同,但每个部分有两个副本,即wwww.example.com/scripts.js和wwww.example.com/admin/scripts.js。对于前端,还有一些额外的用于记录用户的东西 在我的HTML文件中
<div class="g-signin2" data-onsuccess="googleSignIn"></div>
在scripts.js
中function googleSignIn(googleUser) {
console.log('google sign in exec');
var rootDir = $('#pre-nav').data('root');
var id_token = googleUser.getAuthResponse().id_token;
var xhr = new XMLHttpRequest();
xhr.open('POST', rootDir+'includes/google-login.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
console.log('Signed in as: ' + xhr.responseText);
if(xhr.responseText == 1){
window.location.href = rootDir+'sign-up.php';
} else if(xhr.responseText == 2) {
location.reload();
} else if(xhr.responseText.trim().match("^Entered")){
alert(xhr.responseText);
event.preventDefault();
return false;
}else if(xhr.responseText.trim().match("^Something")){
alert(xhr.responseText);
}
};
xhr.send('idtoken=' + id_token);
// Get $id_token via HTTPS POST.
// console.log(id_token);
}
注销在前端工作正常,但在管理部分没有。在管理部分我在控制台中遇到了这个javascript错误:
Uncaught uO {message: "Missing required parameter 'client_id'", stack: "gapi.auth2.ExternallyVisibleError↵ at new uO (h…OahSiUblyIZ1xkzd0Qmcnjj6JLA/cb=gapi.loaded_0:1:15"}
当我点击退出按钮时出现错误:Uncaught TypeError: Cannot read property 'signOut' of null
我做错了什么?