javascript - 从 Google 帐户中提取电子邮件地址

标签 javascript c# asp.net google-api

我正在开发一个网站,客户要求提供允许使用 Google 和 Facebook 帐户注册/登录的选项。如何从用户的 Google 个人资料中提取电子邮件地址以存储在数据库中?

这是我的代码。问题是我没有完全获取用户配置文件。相反,我只收到用户名。

try
{
     WebClient client = new WebClient();
     var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="
         + access_token;

     string outputData = client.DownloadString(urlProfile);

     GoogleUserOutputData serStatus =
         JsonConvert.DeserializeObject<GoogleUserOutputData>(outputData);

     if (serStatus != null)
     {
         return serStatus;
         // You will get the user information here.
     }
 }
 catch (Exception ex)
 {
     //catching the exception
 }

 return null;

最佳答案

这里是一种用 JavaScript 接收数据(电子邮件等)的方法。最后它显示带有数据的警报。 (您可以将此数据存储在数据库中。)这是一个带有 Google 按钮的完整工作示例。

<html>

  <head>
    <title>Demo: Getting an email address using the Google+ Sign-in button</title>
    <!-- Include the API client and Google+ client. -->
    <script src = "https://plus.google.com/js/client:platform.js" async defer></script>
  </head>

  <body>
    <!-- Container with the Sign-In button. -->
    <div id="gConnect" class="button">
      <button class="g-signin"
          data-scope="email"
          data-clientid="Your_Client_ID"
          data-callback="onSignInCallback"
          data-theme="dark"
          data-cookiepolicy="single_host_origin">
      </button>
      <!-- Textarea for outputting data -->
      <div id="response" class="hide">
        <textarea id="responseContainer" style="width:100%; height:150px"></textarea>
      </div>
    </div>
 </body>

  <script>
  /**
   * Handler for the signin callback triggered after the user selects an account.
   */
      function onSignInCallback(resp) {

    gapi.client.load('plus', 'v1', apiClientLoaded);
  }

  /**
   * Sets up an API call after the Google API client loads.
   */
      function apiClientLoaded() {

    gapi.client.plus.people.get({userId: 'me'}).execute(handleEmailResponse);
  }

  /**
   * Response callback for when the API client receives a response.
   *
   * @param resp The API response object with the user email and profile information.
   */
  function handleEmailResponse(resp) {
      var primaryEmail;
      var name;
      var gender;

    for (var i=0; i < resp.emails.length; i++) {
        if (resp.emails[i].type === 'account')
            primaryEmail = resp.emails[i].value;
        if (resp.displayName != null)
            name = resp.displayName;
        gender = resp.gender;
    }
    document.getElementById('responseContainer').value = 'Primary email: ' +
          primaryEmail + '\n\nFull Response:\n' + JSON.stringify(resp);
      ShowAlert("Email: "+primaryEmail +" "+"Name: "+ resp.displayName +" "+"Gender: "+gender);
  }

  </script>

</html>

有关更多信息和详细信息,您可以(应该)阅读此链接: Getting people and profile information

关于javascript - 从 Google 帐户中提取电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45756440/

相关文章:

javascript - Mocha on Node 中的 XMLHttprequest

c# - 列出每个对象在 System.Runtime.Caching 内存缓存中占用的大小 c#

asp.net - Azure Functions 中的 DI

asp.net web.config 加密 - 找不到 RSA key 容器

c# - 在 ASP.NET MVC 中按表单顺序枚举发布的数据

Javascript 和 JQuery Dom 全局变量作用域

javascript - 使用 '>' 字符使用 javascript 拆分字符串

javascript - 当 then 链中 Promise.all 中的 Promise 失败时会发生什么

c# - 无法加载文件或程序集或其依赖项。该系统找不到指定的文件

c# - Bloomberg APIv3 返回缓慢的消费者警告