javascript 退出时 HTML PHP 谷歌单点登录将抛出“无法读取未定义的属性‘getAuthInstance’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30945798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML PHP google single sign on signout will throw "Cannot read property 'getAuthInstance' of undefined"
提问by manu g
I have created google single sign on by following steps mentioned in https://developers.google.com/identity/sign-in/web/sign-in
我按照https://developers.google.com/identity/sign-in/web/sign-in 中提到的步骤创建了 google 单点登录
The sign in works like a charm but when i try to integrate sign out as per the article in the link
登录就像一个魅力,但是当我尝试按照链接中的文章集成注销时
i get the following javascript error in console
我在控制台中收到以下 javascript 错误
Uncaught TypeError: Cannot read property 'getAuthInstance' of undefined
未捕获的类型错误:无法读取未定义的属性“getAuthInstance”
And my signout function looks like
我的注销功能看起来像
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>
and my sign in looks like
我的登录看起来像
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
}
回答by Jaros?aw Gomu?ka
Are signIn and signOut used on the same page? Div g-signin2 loads and inits gapi.auth2 so it should work as long as those are on the same page.
登录和退出是否在同一页面上使用?Div g-signin2 加载并初始化 gapi.auth2,因此只要它们在同一页面上,它就应该可以工作。
In case signOut is on separate page, you should manually load and init gapi.auth2 library.
如果 signOut 在单独的页面上,您应该手动加载和初始化 gapi.auth2 库。
Full example (you have to replace YOUR_CLIENT_ID with your actual client_id):
完整示例(您必须用您的实际 client_id 替换 YOUR_CLIENT_ID):
<html>
<head>
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
</head>
<body>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
function onLoad() {
gapi.load('auth2', function() {
gapi.auth2.init();
});
}
</script>
<a href="#" onclick="signOut();">Sign out</a>
<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
</body>
</html>
回答by Leon Wang
Check: https://developers.google.com/identity/sign-in/web/reference#gapiauth2getauthinstance
检查:https: //developers.google.com/identity/sign-in/web/reference#gapiauth2getauthinstance
and in particular this part:
尤其是这部分:
gapi.auth2.getAuthInstance()
Returns the GoogleAuth object. You must initialize the GoogleAuth object with
gapi.auth2.init()
before calling this method.
gapi.auth2.getAuthInstance()
返回 GoogleAuth 对象。
gapi.auth2.init()
在调用此方法之前,您必须初始化 GoogleAuth 对象。
For me, the problem is that I didn't call gapi.auth2.init()first
对我来说,问题是我没有先调用gapi.auth2.init()
回答by Giorgi Jambazishvili
I had same issue and seems that I found solution for me and I think this should be the case.
我有同样的问题,似乎我为我找到了解决方案,我认为应该是这种情况。
you should have method call like that, at the bottom of the script, like me, below:
你应该有这样的方法调用,在脚本的底部,像我一样,如下:
gapi.load("client", initAuth);
this should be modified like
这应该修改为
gapi.load("client:auth2", initAuth);
and this should work (at least worked for me).
这应该有效(至少对我有用)。
回答by seng
easiest way is to add ?onload=onLoad so that your api script becomes
最简单的方法是添加 ?onload=onLoad 以便您的 api 脚本变为
<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
function onLoad() {
gapi.load('auth2', function() {
gapi.auth2.init();
});
}
</script>