Elizabeth Hankins
Elizabeth Hankins

Reputation: 11

With the Contacts API Migrating to the People API, how can I get a list of contacts with a specific label?

I was getting a list of contacts using the Contact method in Google App Script. As of June 16th, the Contact method is not longer available. I am trying to use the People method but cannot figure out how to get a specific list of email addresses categorized by the label. Please help and TYIA

Original Google App Script:

    var emailList = [];
    var contacts = ContactsApp.getContactGroup('Membership Committee').getContacts();

    for(var i in contacts){
      emailList.push(contacts[i].getEmailAddresses());
    }

Upvotes: 0

Views: 725

Answers (1)

Nikko J.
Nikko J.

Reputation: 5533

As answered by Rafa Guillermo:

  1. Use contactGroups.get to get the group member resource names.
  2. Use people.getBatchGet to make a batch request to the API and use member resource names and personFields: 'emailAddresses' as parameters.

Example:

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);
  })
}

Note: Label ID can be found by clicking a Label and in the URL, any characters after https://contacts.google.com/label/ is the ID.

Output:

enter image description here

References:

Upvotes: 0

Related Questions