google-signin - 将谷歌登录集成到静态 html
问题描述
我正在尝试将谷歌登录页面集成到静态 html 页面。我所要做的就是,当用户点击 google 登录按钮时,他会获得 google 凭据页面,并且在成功验证后,我想在 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);
});
}
推荐阅读
- html - HTML5;仅验证字母 AK 和数字,长度 3
- typescript - TS2345 - 在我的模拟中无法避免 ts-ignore
- php - PHP 'PDOException' 带有消息'SQLSTATE [23000]:完整性约束
- c++ - google breakpad 中剥离的二进制文件没有符号痕迹
- android - 尝试构建一个程序,使用户能够从图库中选择图像,但是当我运行并选择图像时,图像不会出现在应用程序中
- node.js - rpm -i 项目因我的操作系统已安装或似乎不存在的软件包的依赖问题而失败
- excel - 工作表审核变更日志
- testng - RemoteTestNG 在线程“main”java.lang.NoSuchMethodError 中检测到 TestNG 7.3.0 异常:org.testng.TestNG.configure(Lorg/testng/CommandLineArgs
- python - 将xlsx文件的一行中的每个单元格写入txt文件python
- java - 当我在 localhost 上运行我的应用程序时,我能够在 html 上显示图像,但是当我部署到云平台时,图像会得到 404