我正在从事一个需要使用Google电子邮件帐户登录然后重定向到另一个页面的项目。我按照https://developers.google.com/identity/sign-in/web/sign-in中的步骤进行操作,并构建了一个自定义登录按钮。
情况是:
我去了localhost:8080,它显示了一个Google登录按钮。然后,我单击此按钮,它弹出了一个窗口(Google sigin窗口)。我选择了一个电子邮件帐户,然后该窗口消失了,我停留在localhost:8080上。它应该跳到另一页。
这是我的代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="[MY_CLIENT_ID].apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<body>
<p>Please log in with your Google Email Address</p>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<script>
function onSignIn(googleUser) {
console.log("===================");
// 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);
window.location.href = "/resource?email="+profile.getEmail();
}
</script>
</body>
</html>
在将console.log(“ ==========”)放在这里之后,我意识到从未调用过此函数。
有人知道如何解决此错误吗?