将谷歌登录集成到静态 html



我正在尝试将谷歌登录页面集成到静态html页面。我所要做的就是,当用户单击谷歌登录按钮时,他会获得谷歌凭据页面,并且在成功进行身份验证后,我想在 html 页面中显示基本信息值。这是我的代码。我已成功登录,但之后我无法看到打印的值。有什么帮助吗?

<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="<ClientID>">
<title>My Web app</title>
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div id="profileinfo"></div>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<script>
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
alert("ID: " + profile.getId()); // Don't send this directly to your server!
alert('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>
</body>
</html>

你也没有错误处理,这很难调试。

另一种方法是这样做:

function onSignIn () {
var instance = gapi.auth2.getAuthInstance();
instance.signIn().then((response) => {
var user = response.getBasicProfile();
var data = {
id: user.getId(),
email: user.getEmail(),
name: user.getName(),
// etc
}
console.log(data);
},
err => {
// error handling 
console.log(error);
});
}

最新更新