javascript - 谷歌登录没有反应

标签 javascript asp.net google-signin

我通过以下链接将 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/

相关文章:

javascript - 如何在打开下拉菜单时隐藏当前选定的选项

javascript - 如何迭代请求对象中的字符串?

asp.net - DropDownList.SelectedValue 不正确

ios - 谷歌登录 404 错误与 swift

android - react native 谷歌登录开发人员错误代码 10

c# - 发布后谷歌登录不起作用

javascript - 如何在悬停时制作 gif 动画,悬停后恢复正常

javascript - 如果网页是 example.com 网站上的 iframe,它会显示一些数据而不是整页

c# - 我应该在哪里创建我的开发博客?

asp.net - VB恶意软件逆向工程工具