I've created a google app maker app and added a server script in order to access the Admin SDK API. When I preview my app I see the following error "GoogleJsonResponseException: Not Authorized to access this resource/api at listAllGroups". My understanding is that google app maker uses the account of the current user to access resources, which means I need to grant myself API access to the Admin SDK. How can I achieve this? Btw, I'm logged in as a super admin
Here is the server script I'm using:
function listAllGroups() {
var pageToken;
var page;
do {
page = AdminDirectory.Groups.list({
domain: 'test.domain.com',
maxResults: 100,
pageToken: pageToken
});
var groups = page.groups;
if (groups) {
for (var i = 0; i < groups.length; i++) {
var group = groups[i];
Logger.log('%s (%s)', group.name, group.email);
}
} else {
Logger.log('No groups found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
}
If you are using a super admin account, chances are that the domain parameter is giving you the problem. Try this instead:
function listAllGroups() {
var pageToken;
var page;
do {
page = AdminDirectory.Groups.list({
customer: 'my_customer',
maxResults: 100,
pageToken: pageToken
});
var groups = page.groups;
if (groups) {
for (var i = 0; i < groups.length; i++) {
var group = groups[i];
Logger.log('%s (%s)', group.name, group.email);
}
} else {
Logger.log('No groups found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
}
Please note that here we are using the parameter customer with the value my_customer instead of the parameter domain. If these does not fix the issue, then make sure that the app runs as the user and not as the developer. If it runs as the developer, then make sure the developer is a super admin account. Hope it helps!