我正在学习如何使用谷歌登录我的网站。我正在遵循我在这个页面上找到的指南:https://developers.google.com/identity/sign-in/web/
因此,我创建了一个新文档,并复制/传递了他们在上面页面上提出的代码。我在标签中添加了我的客户端ID,并测试了代码。一切正常,我可以登录了。登录按钮发生了变化,显示为登录,而不是登录。此外,在我的控制台中,我可以看到来自console.log()的数据,比如id、电子邮件、姓名……
我继续阅读指南,我发现如何在这个页面上添加一个注销按钮:https://developers.google.com/identity/sign-in/web/sign-in我实现了这段代码,下面是我拥有的完整代码(没有客户端ID)。
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<a href="#" onclick="signOut();">Sign out</a>
<script>
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // Don't send this directly to your server!
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
};
</script>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>
</body>
</html>当我点击它时,我得到的是“用户已注销”。在我的控制台上。但是,如果我刷新页面,google按钮仍然会显示我已登录,并且配置文件信息将在我的控制台中返回-即使我手动清除了控制台。注销按钮显然不起作用,我也不知道为什么。
有谁有这方面的经验吗?
谢谢你的帮助!
发布于 2017-07-20 08:47:06
为了调用signOut,您需要调用authorize()来获取用于将用户标记为已注销的id_token。
参见google-api-javascript-client GitHub上的this issue。
这里发布了一个代码示例,说明您需要首先使用一个调用gapi.auth.signOut()的回调函数来调用gapi.auth.authorize。
从本质上讲,就像这样:
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES,
'immediate': false,
cookie_policy: 'single_host_origin',
response_type: 'token id_token'
},
function (authResult) { gapi.auth.signOut();}
);https://stackoverflow.com/questions/45203124
复制相似问题