javascript - 如何将 google 登录集成到我的网站并收集用户详细信息?

标签 javascript php html google-signin

我想将 google 登录按钮集成到 my website 上.我知道 html,但我不确定 php 和 java 脚本。最终,我希望谷歌登录以登录用户并向我提供他们的信息,以便我可以将其安全地存储在我的 phpmyadmin 数据库中。我去过the google tutorial对此却发现并没有说明如何全面收集用户信息。我试图遵循这个,到目前为止我有 this但它远非应有的样子。我看过其他教程,例如 this one .但是我发现他们都不遵循谷歌的说明,例如你必须在其中下载谷歌 API,但是在谷歌网站上它没有提到下载任何东西。

下面是what i have managed to do so far的代码通过使用谷歌教程:

<html lang="en">
  <head>
    <meta name="google-signin-scope" content="profile email">
    <meta name="google-signin-client_id" content="808271051181-424qcdq0emrd0pd77frfiuacvcetp58t.apps.googleusercontent.com">
    <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('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>

最佳答案

您可能能够从 javascript 的配置文件中获取所有数据,但我想将所有数据放入 php 变量中,以便我可以存储在我的数据库中。为此,我将 google id token 作为来自 javascript 的发布数据发送(怎么做 here )。 您仍然需要您拥有的所有其他谷歌登录代码,但我将 onSingIn 替换为:

function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();
    document.getElementById("userid").value = googleUser.getAuthResponse().id_token;
    document.getElementById("userid").form.submit();
}

同样在正文中添加表单代码:

<form action="login.php" method="post">
    <input type="hidden" name="id" id="userid">
</form>

然后你需要另一个我称之为 login.php 的文件,它包含以下函数:

function get_var($var)
{
    $id = $_POST["id"]; // id from google
    $id_token = file("https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" . $id); // decrypted id
    foreach ($id_token as $part) {
        // part is a factor of the user such as name or email
        // remove unecessary charcters
        $peice = str_replace("\"", "", $part);
        $peice = str_replace(",", "", $peice);
        $peice = substr($peice, 0, strpos($peice, ":") + 2);
        if (strpos($peice, $var) !== false) {
            $var = str_replace("\"", "", $part);
            $var = str_replace(",", "", $var);
            $var = substr($var, strpos($var, ":") + 2);
            return $var;
        }
    }
}

有了它,您应该能够获得所需的所有信息。使用示例:

$name = trim(get_var("name"));
$email = trim(get_var("email"));

要查看所有可访问信息,请在 get_var 中打印出 $id_token 或转到 https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=最后添加一个 id 标记。 有关从 ID token 获取数据的更多信息 here .

关于javascript - 如何将 google 登录集成到我的网站并收集用户详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51411652/

相关文章:

html - 为什么 Jumbotron 看起来太大而不适合 window ?

javascript - fullPage.js 滚动幻灯片到不同方向

javascript - jQuery 动态添加选定值及其父数据,如格式化

javascript - Mustache 动态设置变量

php - SQL 查询在 phpMyAdmin 中有效,但在 php 页面中无效

html - CSS/HTML 中没有可见宽度的侧边栏

html - div 之间的间距(边距?)

javascript - 学习了react和redux之后,我们还需要在组件中声明状态吗?

php - 如果最大时间戳大于其他最大时间戳

PHP使用flock()的计数器问题