我通过以下链接将 Google 登录集成到我的 ASP.NET 应用程序中:https://developers.google.com/identity/sign-in/web/
这是代码片段:
<html lang="en">
<head>
<title></title>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="**REDACTED**">
<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>
<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("Name: " + profile.getName());
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>
当我单击登录按钮时,我会看到权限页面。但是当我单击“允许”时,控制台日志为空。不过,从代码中可以清楚地看出,它应该填写登录名、id、电子邮件。我在这里缺少什么?请帮忙
最佳答案
如果 onsuccess 回调没有被触发,就像您的登录没有成功一样。通过添加 onfailure 回调进行验证。
<div class="g-signin2" data-onsuccess="onSignIn" data-onfailure="onFailure" data-theme="dark"></div>
<script>
function onFailure() { console.error('Sign in has failed!'); }
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("Name: " + profile.getName());
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>
关于javascript - 谷歌登录没有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34783712/