People.People.SearchDirectoryPeople()查询带有多个电子邮件地址的查询
在Google Apps脚本中,我需要查询许多同事的Google用户个人资料图片URL。
这是一个单个用户的工作示例:
searchDirectoryPeople('[email protected]');
function searchDirectoryPeople(query) {
const options = {
query: query,
readMask: 'photos,emailAddresses',
sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']
}
const people = People.People.searchDirectoryPeople(options);
if(people && people.people) {
Logger.log('size: '+people.people.length);
people.people.forEach(person => {
let url = '';
let email = '';
if(person) {
if(person.photos && person.photos[0]) {
url = person.photos[0].url;
}
if(person.emailAddresses && person.emailAddresses.length) {
person.emailAddresses.forEach(item => {
if(item.metadata && item.metadata.sourcePrimary) {
email = item.value;
}
});
}
}
Logger.log('email: '+email+': '+url);
//Logger.log('person: %s', JSON.stringify(person, null, 2));
});
} else {
Logger.log('no people.people');
}
}
我发现我可以查询所有jimmy
人员:
searchDirectoryPeople('jimmy');
我有所有员工的电子邮件地址。我可以一一循环浏览1000多名员工的大列表,但这是不实际的。我正在寻找一种查询多个电子邮件地址的方法。 The docs at https://developers.google.com/people/api /REST/V1/PEOPE/SEERCEDDIRECTORYPEOPLE 对于查询而言是隐秘的。 I tried many things like these but nothing works:
'[email protected], cgi/l/电子邮件保护“ class =” __ cf_email__“ data-cfemail =”
- 73101A1D170A5D051C051C0107160B333160B121E031E031F165D101C1C1C1C1C1E /电子邮件保护“ class =” __ cf_email__'数据-cfemail =“ 127173607E3C657A7777777776052776A737F627E7773C717D7F”> [email  proted] 或或 7C098819C9A8B96AE8B968F839E828BC08D8183“> [email  pretanded ]'
I am looking for a query by list of email addresses as input, such as:
[ '[email protected]', '[email protected]', '[email protected]' ]
Is it possible to have an OR
query in People.People. SearchDirectoryPeople()
?
更新2022-05-31
我尝试通过所有电子邮件进行循环,然后遇到配额限制或脚本运行时限制。
@lorena Gomez的答案是正确的:首先使用people.people.listDirectoryPeople()
获取所有电子邮件地址的资源名称,然后是people.people.getBatchget()
要通过资源名称获取个人资料图片URL。前者每个呼叫的限制为1000名员工,后者将其限制为200。在我们的情况下,我们有1K+电子邮件地址作为输入,而由list> listDirectoryPeople()返回的20K+员工()
。
工作代码:
const emails = [
'[email protected]',
'[email protected]',
'[email protected]'
];
let emailToUrl = getGoogleProfilePictureUrls(emails);
Logger.log('emailToUrl: %s', JSON.stringify(emailToUrl, null, 2));
// expected output:
// emailToUrl: {
// "[email protected]": "https://lh3.googleusercontent.com/a-/xxxx=s100",
// "[email protected]": "https://lh3.googleusercontent.com/a-/xxxx=s100",
// "[email protected]": "https://lh3.googleusercontent.com/a-/xxxx=s100"
// }
function getGoogleProfilePictureUrls(emails) {
let options = {
readMask: 'emailAddresses',
sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE'],
pageSize: 1000
}
let run = 1;
let resourceNameToEmails = {};
let result = {};
while(run === 1 || result.nextPageToken) {
if(result.nextPageToken) {
options.pageToken = result.nextPageToken;
}
result = People.People.listDirectoryPeople(options);
Logger.log('request #' + (run++) + ', got '+result.people.length+' resource names');
result.people.forEach(person => {
if(person.emailAddresses) {
person.emailAddresses.forEach(obj => {
if(obj.metadata && obj.metadata.sourcePrimary) {
let email = obj.value
if(emails.indexOf(email) >= 0) {
resourceNameToEmails[person.resourceName] = email;
}
}
});
}
});
Utilities.sleep(200);
}
run = 1;
let emailToUrl = {};
let resourceNames = Object.keys(resourceNameToEmails);
let resourceNameBatch = resourceNames.splice(0, 200);
while(resourceNameBatch.length) {
options = {
personFields: 'photos',
resourceNames: resourceNameBatch,
sources: [ 'READ_SOURCE_TYPE_PROFILE' ]
};
result = People.People.getBatchGet(options);
if(result && result.responses) {
Logger.log('request #' + (run++) + ', got '+result.responses.length+' urls');
result.responses.forEach(person => {
let primaryUrl = '';
let url = '';
if(person.person && person.person.photos) {
person.person.photos.forEach(photo => {
if(photo.metadata && photo.metadata.source && photo.metadata) {
url = photo.url;
if(photo.metadata.source.type === 'PROFILE' && photo.metadata.primary) {
primaryUrl = url;
}
}
});
}
let email = resourceNameToEmails[person.person.resourceName];
emailToUrl[email] = primaryUrl || url;
});
}
Utilities.sleep(200);
resourceNameBatch = resourceNames.splice(0, 200);
}
return emailToUrl;
}
In a Google Apps Script I need to query the Google user profile picture URL of many coworkers.
Here is a working example for a single user:
searchDirectoryPeople('[email protected]');
function searchDirectoryPeople(query) {
const options = {
query: query,
readMask: 'photos,emailAddresses',
sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']
}
const people = People.People.searchDirectoryPeople(options);
if(people && people.people) {
Logger.log('size: '+people.people.length);
people.people.forEach(person => {
let url = '';
let email = '';
if(person) {
if(person.photos && person.photos[0]) {
url = person.photos[0].url;
}
if(person.emailAddresses && person.emailAddresses.length) {
person.emailAddresses.forEach(item => {
if(item.metadata && item.metadata.sourcePrimary) {
email = item.value;
}
});
}
}
Logger.log('email: '+email+': '+url);
//Logger.log('person: %s', JSON.stringify(person, null, 2));
});
} else {
Logger.log('no people.people');
}
}
I found out that I can query all jimmy
people:
searchDirectoryPeople('jimmy');
I have the email address of all employees. I could loop through a big list of 1000+ employees one by one, but this is not practical. I am looking for a way to query multiple email addresses. The docs at https://developers.google.com/people/api/rest/v1/people/searchDirectoryPeople are cryptic for the query. I tried many things like these but nothing works:
'[email protected], [email protected], [email protected]'
'[email protected] OR [email protected] OR [email protected]'
I am looking for a query by list of email addresses as input, such as:
[ '[email protected]', '[email protected]', '[email protected]' ]
Is it possible to have an OR
query in People.People.searchDirectoryPeople()
?
UPDATE 2022-05-31
I tried looping through all emails and ran either into a quota limit or a script runtime limit.
@Lorena Gomez's answer is correct: First use the People.People.listDirectoryPeople()
to get the resource names of all email address, followed by People.People.getBatchGet()
to get the profile picture URL by resource names. The former limits to 1000 employees per call, the latter limits to 200. This works in our case where we have 1k+ email addresses as input, and 20k+ employees returned by listDirectoryPeople()
.
Working code:
const emails = [
'[email protected]',
'[email protected]',
'[email protected]'
];
let emailToUrl = getGoogleProfilePictureUrls(emails);
Logger.log('emailToUrl: %s', JSON.stringify(emailToUrl, null, 2));
// expected output:
// emailToUrl: {
// "[email protected]": "https://lh3.googleusercontent.com/a-/xxxx=s100",
// "[email protected]": "https://lh3.googleusercontent.com/a-/xxxx=s100",
// "[email protected]": "https://lh3.googleusercontent.com/a-/xxxx=s100"
// }
function getGoogleProfilePictureUrls(emails) {
let options = {
readMask: 'emailAddresses',
sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE'],
pageSize: 1000
}
let run = 1;
let resourceNameToEmails = {};
let result = {};
while(run === 1 || result.nextPageToken) {
if(result.nextPageToken) {
options.pageToken = result.nextPageToken;
}
result = People.People.listDirectoryPeople(options);
Logger.log('request #' + (run++) + ', got '+result.people.length+' resource names');
result.people.forEach(person => {
if(person.emailAddresses) {
person.emailAddresses.forEach(obj => {
if(obj.metadata && obj.metadata.sourcePrimary) {
let email = obj.value
if(emails.indexOf(email) >= 0) {
resourceNameToEmails[person.resourceName] = email;
}
}
});
}
});
Utilities.sleep(200);
}
run = 1;
let emailToUrl = {};
let resourceNames = Object.keys(resourceNameToEmails);
let resourceNameBatch = resourceNames.splice(0, 200);
while(resourceNameBatch.length) {
options = {
personFields: 'photos',
resourceNames: resourceNameBatch,
sources: [ 'READ_SOURCE_TYPE_PROFILE' ]
};
result = People.People.getBatchGet(options);
if(result && result.responses) {
Logger.log('request #' + (run++) + ', got '+result.responses.length+' urls');
result.responses.forEach(person => {
let primaryUrl = '';
let url = '';
if(person.person && person.person.photos) {
person.person.photos.forEach(photo => {
if(photo.metadata && photo.metadata.source && photo.metadata) {
url = photo.url;
if(photo.metadata.source.type === 'PROFILE' && photo.metadata.primary) {
primaryUrl = url;
}
}
});
}
let email = resourceNameToEmails[person.person.resourceName];
emailToUrl[email] = primaryUrl || url;
});
}
Utilities.sleep(200);
resourceNameBatch = resourceNames.splice(0, 200);
}
return emailToUrl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
方法:People.SearchDirectoryPeople
您一次只能指定一个人。另一个选项可以是 poys.pepoy.people.people.getBatchget()额外的一步,但为您提供有关您指定的人列表的信息。该请求看起来像这样:
您可以使用方法:people.ListDirectoryPeople
It looks like with
Method: people.searchDirectoryPeople
you can only specify one person at a time.Another option could be People.People.getBatchGet() which will require an extra step but provides you information about a list of the people you specify. The request would look something like this:
You can get the user's
account_id
with Method: people.listDirectoryPeople怎么样?
How about this?