【问题标题】:With the Contacts API Migrating to the People API, how can I get a list of contacts with a specific label?通过将联系人 API 迁移到人员 API,我如何获取具有特定标签的联系人列表?
【发布时间】:2021-06-23 17:09:59
【问题描述】:
我正在使用 Google App 脚本中的 Contact 方法获取联系人列表。自 6 月 16 日起,联系方式不再可用。我正在尝试使用 People 方法,但无法弄清楚如何获取按标签分类的特定电子邮件地址列表。请帮助和TYIA
原始谷歌应用脚本:
var emailList = [];
var contacts = ContactsApp.getContactGroup('Membership Committee').getContacts();
for(var i in contacts){
emailList.push(contacts[i].getEmailAddresses());
}
【问题讨论】:
标签:
google-apps-script
google-contacts-api
google-people-api
【解决方案1】:
Rafa Guillermo回答:
- 使用
contactGroups.get 获取组成员资源名称。
- 使用
people.getBatchGet 向API 发起批量请求,并使用成员资源名称和personFields: 'emailAddresses' 作为参数。
示例:
function myFunction() {
var groupId = "Insert Group/Label ID here"
var contactGroup = People.ContactGroups.get('contactGroups/'+groupId, {"maxMembers" : 50});
var memberResourceNames = contactGroup.memberResourceNames;
var test = People.People.getBatchGet({
resourceNames: memberResourceNames,
personFields: 'emailAddresses'
})
test.responses.forEach(res => {
Logger.log(res.person.emailAddresses[0].value);
})
}
注意:标签 ID 可以通过点击一个标签找到,在 URL 中,https://contacts.google.com/label/ 之后的任何字符都是 ID。
输出:
参考资料: