I'm making the transition from Contacts API to People API as required by Google. I'm very surprised by the lack of examples online but I can now create, delete and modify/update (thanks to the reply by @Tanaike's reply to Omar F. here: Cannot update contactusgin People api using Google Apps Script) a contact, with delete and modify via iterating through People.People.Connections.List(), with its max of 100 results (from the Apps Script sample here: https://developers.google.com/people/quickstart/apps-script).
But I have about 12,000 contacts and need to use (I assume) People.People.searchContacts()
. I haven't found any Apps Script examples and the documentation is all in Java, which I don't know how to translate to Apps Script:
...
// Send search request
SearchResponse response = peopleService.people().searchContacts()
.setQuery("query")
.setReadMask("names,emailAddresses")
.execute();
or "Protocol" which is similarly not useful (for me - I've tried!):
...
// Send search request after several seconds
GET /v1/people:searchContacts?query=query&readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com
My best guess to date using Google Drive Query Parameter style since the People API link from this page (https://developers.google.com/people/v1/query-parameters) doesn't seem to help:
let searchResp = People.People.searchContacts(
"phoneNumbers contains 5632",
{ readMask: "names,phoneNumbers,addresses,emailAddresses" }
);
let personBob = searchResp.results.find(p =>
p.names[0].displayName.includes("Bob Q22222")
);
So after spending an embarrassing amount of time getting to this point, I'm throwing in the towel and hoping someone can give an example of getting a searchResponse via People.People.searchContacts()
to show what
- A search query looks like and
- What a readMask looks like.
I understand that the searchResponse.results
is just an array of People and should be fine from there.
Thank you and I hope this wasn't too waffly (baffled non-programmer for our little husband & wife business). ~
Edit: Almost there
Modified from Rafi's reply, this works from a form-attached Appscript file, which is what I want but it's not retrieving all results, see further below):
function run() {
let query = "Q12755";
let found = People.People.searchContacts({
"query": query,
"readMask": "names,addresses,emailAddresses,phoneNumbers"
});
if (found.results) {
for (i = 0; i < found.results.length; i++) {
let thisResult = found.results[i];
Logger.log(`Result ${i}: ${thisResult.person.names[0].displayName}`);
}
} else {
Logger.log(`No results for ${query}`);
}
}
...but, it's not picking up all of the results.
The test function and result for "Q12755" query:
Two results for that string in Google Contacts:
And the same two results on my phone:
Any idea why not all results are being retrieved? I've tested several queries and it consistently misses results. All (I believe) of the Contacts tested have been created via an Appscript script via the deprecating Contacts API.
P.S. I might add that it's not an issue with it only retrieving one result. In other cases where there "should" be like 20 results, in has retrieved half a dozen or so.
P.P.S. A "warmup" of the cache is recommended/required, and I'm assuming the updated script below does that okay. There's no difference to the results:
function run() {
let blankQueryForWarmUp = "";
let found = People.People.searchContacts({
"query": blankQueryForWarmUp,
"readMask": "names,addresses,emailAddresses,phoneNumbers"
});
Utilities.sleep(5000);
let query = "Q12755";
found = People.People.searchContacts({
"query": query,
"readMask": "names,addresses,emailAddresses,phoneNumbers"
});
if (found.results) {
for (i = 0; i < found.results.length; i++) {
let thisResult = found.results[i];
Logger.log(`Result ${i}: ${thisResult.person.names[0].displayName}`);
}
} else {
Logger.log(`No results for ${query}`);
}
}