Created
August 25, 2021 08:17
-
-
Save ricardodantas/524e3ac9ec2573600b1479110fd98810 to your computer and use it in GitHub Desktop.
Fetch user groups using an impersonated Service Account from Google Admin drectory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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: '[email protected]', | |
}); | |
(async () => { | |
try { | |
const admin = await google.admin({ | |
version: 'directory_v1', | |
auth | |
// auth: client, | |
}); | |
const result = await admin.groups.list({ | |
userKey: '[email protected]', | |
}) | |
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