如何使用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>
我也尝试了this other unanswered question about the Google People API中的方法。
答案 0 :(得分:2)
请做一次
using System.Xml.Serialization;
using System.IO;
public static class SettingsIO
{
public static void WriteSettings(AppSettings settings, string settingsFilePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(AppSettings));
if (!Directory.Exists(Path.GetDirectoryName(settingsFilePath)))
Directory.CreateDirectory(Path.GetDirectoryName(settingsFilePath));
using (StreamWriter SW = new StreamWriter(settingsFilePath))
serializer.Serialize(SW, settings);
}
public static AppSettings GetSettings(string settingsFilePath)
{
AppSettings m_Settings = null;
if (File.Exists(settingsFilePath))
{
XmlSerializer serializer = new XmlSerializer(typeof(AppSettings));
using (FileStream FS = new FileStream(settingsFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader SR = new StreamReader(FS))
m_Settings = (AppSettings)serializer.Deserialize(SR);
}
if (m_Settings == null)
m_Settings = new AppSettings();
return m_Settings;
}
}
在这里:
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>');
});
}