If you are using Node.Js you can install this library:
googleapis@105 @google-cloud/[email protected]
With this command for example:
yarn add googleapis@105 @google-cloud/[email protected]
Then you can use a script like this one which uses a recursive function to find all groups to which a user belongs:
const fs = require('fs').promises;
const {authenticate} = require('@google-cloud/local-auth');
const path = require('path');
const {google} = require('googleapis');
const SCOPES = [
'https://www.googleapis.com/auth/admin.directory.group.member.readonly',
'https://www.googleapis.com/auth/admin.directory.group.readonly'
];
const CREDENTIALS_PATH = path.join(process.cwd(), '../client_secret_165820051388-0058a80cn1kgh4uk0ediif68t9m9n2ou.apps.googleusercontent.com.json');
const TOKEN_PATH = path.join(process.cwd(), 'token.json');
/**
* Reads previously authorized credentials from the save file.
*
* @return {Promise<OAuth2Client|null>}
*/
async function loadSavedCredentialsIfExist() {
try {
const content = await fs.readFile(TOKEN_PATH);
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}
/**
* Serializes credentials to a file comptible with GoogleAUth.fromJSON.
*
* @param {OAuth2Client} client
* @return {Promise<void>}
*/
async function saveCredentials(client) {
const content = await fs.readFile(CREDENTIALS_PATH);
const keys = JSON.parse(content);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
});
await fs.writeFile(TOKEN_PATH, payload);
}
/**
* Load or request or authorization to call APIs.
*
*/
async function authorize() {
let client = await loadSavedCredentialsIfExist();
if (client) {
return client;
}
client = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (client.credentials) {
await saveCredentials(client);
}
return client;
}
/**
* Lists all groups of a user recursively.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
* @oaram {string} userKey The user email
*/
async function listUsers(auth, userKey) {
const service = google.admin({version: 'directory_v1', auth});
const accumulatedGroups = []
const MAX_RESULTS = 5
const orderBy = 'email'
const res = await service.groups.list({
userKey,
maxResults: MAX_RESULTS,
orderBy
});
const { groups, nextPageToken } = res.data
let pageToken = nextPageToken
if(!!groups) {
accumulatedGroups.push(...groups)
}
while(!!pageToken) {
const res = await service.groups.list({
userKey,
maxResults: MAX_RESULTS,
pageToken,
orderBy
});
const { groups, nextPageToken } = res.data
pageToken = nextPageToken
accumulatedGroups.push(...groups)
}
if(accumulatedGroups.length === 0) {
return []
}
return [
...(await Promise.all(accumulatedGroups.map(g => g.email).map(e => listUsers(auth, e))))
.flatMap(a => a),
...accumulatedGroups
]
}
authorize()
.then((client) => {
console.log("Authentication saved")
return listUsers(client, '[email protected]')
}).then(groups => {
groups.forEach(g => console.log(g.email))
}).catch(console.error)
This script will print out all emails of all groups to which a user with a specific email belongs. However make sure you have the right permissions for the user which created the Google App for which the Oauth credentials were generated. Otherwise you will get a 403 error.