Это работает для меня:
Пример кода:
...
var loggedUser = Session.getActiveUser().getEmail();
var domain = loggedUser.split('@')[1];
var pageToken;
var emails=[];
do {
page = AdminDirectory.Users.list({
domain: domain,
maxResults: 100,
// pageToken: pageToken,
});
var url='https://www.googleapis.com/admin/directory/v1/users?domain='+domain;
var options = {
"method": "get",
"pageToken": pageToken,
"muteHttpExceptions": true,
"headers": {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
"contentType": "application/json",
}
var page = JSON.parse(UrlFetchApp.fetch(url,options));
Logger.log(page);
var users = page.users;
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
emails.push(user.primaryEmail);
}
} else {
Logger.log('No users found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
...
appsscript.json:
{
"timeZone": "America/Los_Angeles",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "AdminDirectory",
"serviceId": "admin",
"version": "directory_v1"
}, {
"userSymbol": "Gmail",
"serviceId": "gmail",
"version": "v1"
}]
},
"webapp": {
"access": "DOMAIN",
"executeAs": "USER_DEPLOYING"
},
"oauthScopes": ["https://www.googleapis.com/auth/gmail.addons.execute", "https://www.googleapis.com/auth/gmail.addons.current.message.metadata", "https://www.googleapis.com/auth/gmail.modify", "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/admin.directory.user" ],
"urlFetchWhitelist": [
"https://www.googleapis.com/admin/directory/v1/users?domain="
],
"executionApi": {
"access": "MYSELF"
},
"gmail": {
"name": "XXX",
"logoUrl": "https://www.gstatic.com/images/icons/material/system/1x/XXX.png",
"contextualTriggers": [{
"unconditional": {
},
"onTriggerFunction": "XXX"
}],
"primaryColor": "#4285F4",
"secondaryColor": "#4285F4",
"openLinkUrlPrefixes": ["https://mail.google.com/"]
}
}