如何从Google登录api提取电子邮件ID?

问题描述 投票:0回答:1

我在构建Web应用程序时实施了google登录。在那里,我保留了Google登录名的选项,但我需要知道登录的用户是我的应用程序的现有用户,否则他必须先注册。

所以我需要知道电子邮件ID并将其与数据库匹配。

我知道这适用于大多数Web应用程序,但是我还没有找到任何源代码,我认为很难自己将获得电子邮件ID的部分编码出来。

这是我使用的Google登录API:

<! DOCTYPE html>
<html>
    <head>
        <title>
          Google Sign in Page
        </title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
        <meta name="google-signin-client_id" content="540496349919-20gn1hjm7i265i27rs9d3dchjv45ni33.apps.googleusercontent.com">
        <script src="https://apis.google.com/js/platform.js" async defer></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="script.js"></script>
        <style>
           .g-signin2 {
           margin-left: 500px;
           margin-top: 200px;
           }
           .data {
              display: none;
           }
        </style>
    </head>
    <body>
         <div class="g-signin2" data-onsuccess="onsignin"></div> 

Here on log in  I will match the user's email-ID with my database list of email-ID's, if not present, he has to register and be directed to a page(say signup.php), if present, he will be directed to his page(say profile.php)

         <div class="data">
            <button onclick="signout()" class="btn btn-danger"> Sign Out </button>
    </body>

</html>

如何获取登录用户的电子邮件ID?

html google-oauth google-login
1个回答
0
投票

使用默认范围使用Google登录用户后,您可以访问用户的Google ID,名称,个人资料URL和电子邮件地址。

要检索用户的个人资料信息,请使用getBasicProfile()方法。

// auth2 is initialized with gapi.auth2.init() and a user is signed in.

    if (auth2.isSignedIn.get()) {
      var profile = 
      auth2.currentUser.get().getBasicProfile();
      console.log('Full Name: ' + profile.getName());
      console.log('Email: ' + profile.getEmail());
    }
© www.soinside.com 2019 - 2024. All rights reserved.