Skip to content

Instantly share code, notes, and snippets.

@ricardodantas
Created August 25, 2021 08:17
Show Gist options
  • Save ricardodantas/524e3ac9ec2573600b1479110fd98810 to your computer and use it in GitHub Desktop.
Save ricardodantas/524e3ac9ec2573600b1479110fd98810 to your computer and use it in GitHub Desktop.
Fetch user groups using an impersonated Service Account from Google Admin drectory.
/*
* Fetch users groups through an impersonated Service Account
*/
const { google } = require('googleapis');
const credentials = require('../service-account-credentials.json')
const scopes = [
'https://www.googleapis.com/auth/admin.directory.user.readonly',
'https://www.googleapis.com/auth/admin.directory.group.readonly',
]
const auth = new google.auth.JWT({
email: credentials.client_email,
key: credentials.private_key,
scopes,
subject: 'admin@domain.com',
});
(async () => {
try {
const admin = await google.admin({
version: 'directory_v1',
auth
// auth: client,
});
const result = await admin.groups.list({
userKey: 'user@domain.com',
})
const { groups } = result.data
console.log(groups);
} catch (error) {
console.log('=====> Error: ', error)
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment