You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js调用Google People API获取联系人报错:personFields掩码必填

解决People API people.people.get()的"personFields mask is required"报错

嘿,这个问题我熟!你遇到的报错是因为People API的people.get方法有个强制要求的参数没传——personFields。和Gmail API不同,People API要求你明确指定需要获取哪些联系人/用户字段,这样能避免返回冗余数据,提升请求效率。

错误原因

personFields是一个必填的掩码参数,需要传入逗号分隔的字段名称列表,告诉API你要获取Person资源中的哪些部分(比如姓名、邮箱、电话等)。你的代码里刚好漏掉了这个参数,所以API直接返回了错误。

修正后的代码

如果你是要获取单个用户/联系人的信息(比如当前登录用户自己,或者某个特定联系人),只需要在请求参数里加上personFields即可。这里给你一个示例,包含常用的字段:

var people = google.people('v1');
people.people.get({
  auth: auth,
  resourceName: 'people/me', // 用'people/me'代表当前登录用户,替换成特定联系人的resourceName可获取该联系人信息
  personFields: 'names,emailAddresses,phoneNumbers,photos' // 按需添加你需要的字段
}, function(err, response) {
  if (err) {
    console.error('调用People API失败:', err);
    return;
  }
  console.log('获取到的用户/联系人信息:', response.data);
});

额外提示:如果要获取所有联系人列表

看你提到要“获取谷歌联系人”,如果你的需求是获取全部联系人,而不是单个联系人的信息,那应该用people.connections.list()方法,这个方法同样需要指定personFields,示例代码如下:

people.connections.list({
  auth: auth,
  resourceName: 'people/me', // 固定为'people/me',代表获取当前用户的联系人列表
  personFields: 'names,emailAddresses,phoneNumbers',
  pageSize: 50 // 可选,控制每页返回的联系人数量,默认是100
}, function(err, response) {
  if (err) {
    console.error('获取联系人列表失败:', err);
    return;
  }
  const contacts = response.data.connections || [];
  if (contacts.length === 0) {
    console.log('没有找到任何联系人');
  } else {
    console.log('联系人列表:');
    contacts.forEach(contact => {
      const displayName = contact.names?.[0]?.displayName || '无姓名';
      console.log(`- ${displayName}`);
      contact.emailAddresses?.forEach(email => console.log(`  邮箱: ${email.value}`));
    });
  }
});

注意事项

  • 确保你指定的personFields是有效的,比如names、emailAddresses、phoneNumbers、addresses、photos都是合法字段,不要填写不存在的字段,否则会触发其他错误。
  • 如果是获取特定联系人的信息,你需要先知道该联系人的resourceName(格式类似people/1234567890abcdef),通常可以通过connections.list()接口获取所有联系人的resourceName。

内容的提问来源于stack exchange,提问作者Apoorva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:36:01