如何整合google登录我的网站并收集用户详细信息?

时间:2018-07-18 22:06:27

标签: javascript php html google-signin

我想将Google登录按钮集成到my website上。我知道html,但不确定php和java脚本。最终,我希望google登录来登录用户并提供他们的信息,以便我可以将其安全地存储在phpmyadmin上的数据库中。为此,我已经访问了the google tutorial,但是发现它并没有说明如何完全收集用户信息。我尝试遵循此方法,到目前为止,我已经拥有this了,但这与应有的样子相去甚远。我看过其他教程,例如this one。但是我发现它们都不遵循google的说明,例如您必须在其中下载google API,但是在google网站上却没有提及下载任何内容。

下面是使用Google教程的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>

1 个答案:

答案 0 :(得分:0)

您也许可以从javascript的配置文件中获取所有数据,但我想将其所有数据都存储到php变量中,以便可以将其存储在数据库中。为此,我从javascript发送了Google ID令牌作为发布数据(该方法here)。 您仍然需要您拥有的所有其他Google登录代码,但我将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"));

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