0

如何使用 Google People API 获取登录用户的电子邮件地址?

我正在尝试检索登录用户的电子邮件地址,以便可以使用它与我的应用程序中的数据进行比较。我已经尝试了各种方法来获取它,但下面的代码只是返回undefined.

      function makeApiCall() {
    gapi.client.people.people.get({
      'resourceName': 'people/me',
      'requestMask.includeField': 'person.names,person.emailAddresses'
    }).then(function(resp) {
      var name = resp.result.names[0].givenName;
      var email = resp.result.emailAddresses[0].emailAddress;
      authorizeButton.insertAdjacentHTML('beforebegin', '<small rel="' + email + '">Logged in as ' + name + '</small>');
    });
  }

将其更改var email = resp.result.emailAddresses[0];[object Object].

我也尝试过使用JSON.parse(),但出现以下错误:

Uncaught SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at (index):285
at h.r2 (cb=gapi.loaded_0:119)
at xs (cb=gapi.loaded_0:122)
at Wq (cb=gapi.loaded_0:122)
at _.C.uea (cb=gapi.loaded_0:121)
at Ap (cb=gapi.loaded_0:115)
at <anonymous>

我还尝试了有关 Google People API 的其他未回答问题中的方法。

4

1 回答 1

3

请做一次

console.log(response.result);

在这里 :

function makeApiCall() {
    gapi.client.people.people.get({
      'resourceName': 'people/me',
      'requestMask.includeField': 'person.names,person.emailAddresses'
    }).then(function(resp) {

      console.log(response.result);
      var name = resp.result.names[0].givenName;
      //var email = resp.result.emailAddresses[0].emailAddress;
      //authorizeButton.insertAdjacentHTML('beforebegin', '<small rel="' + email + '">Logged in as ' + name + '</small>');
    });
  }

之后使用适用的正确对象。

您可能想使用

var email = resp.result.emailAddresses[0].value;
于 2017-11-08T12:21:34.990 回答