I'm new to google apps scripts and I'm running into an error I cant figure out how to correct. The script below runs correctly scrips apps editor, but when I call the function from my spreadsheet I get an error that line 6 (the call to AdminDirectory.Groups.list) is missing required auth credentials.
function listAllGroups() {
let pageToken; //this will store the json responce
let page;
var retArray = [];
do {
page = AdminDirectory.Groups.list({
domain: 'mydomain.com',
maxResults: 400,
pageToken: pageToken
});
const groups = page.groups;
if (!groups) {
Logger.log('No groups found.');
return;
}
// Print group name and email.
for (const group of groups) {
//Logger.log('%s (%s)', group.name, group.email);
retArray.push(group.name); //add the current group name to the array
}
pageToken = page.nextPageToken;
} while (pageToken);
Logger.log(retArray);
return retArray;
}
When I run it in the editor console the second to the last statement Logger.log() does show me the list of groups I want, but like I said I get the error when I call listAllGrous() from my spreadsheet.