ARTICLE AD BOX
I am using the Google Admin API, via node.js. While I can add a users individually to a group, it isn't clear how to batch add them?
I did look around the documentation, but still a little confused. Right now I am needing to add them individually (sync to a staff membership list in our application) and a weekly sync can involve thousands of users, which is very slow doing each user individually.
My current code:
import { Auth, admin_directory_v1 as directoryV1 } from 'googleapis'; class GoogleGroupsService { private async getAdminApi (): Promise<directoryV1.Admin> { try { const scopes = [ 'https://www.googleapis.com/auth/admin.directory.group', 'https://www.googleapis.com/auth/admin.directory.group.member' ]; const jwtClient = new Auth.JWT( this.keyConfig.clientEmail, undefined, this.keyConfig.privateKey.replace(/\\n/g, '\n'), scopes ); const adminApi = new Admin({ auth: jwtClient }); await jwtClient.authorize(); return adminApi; } catch (error) { logger.error('Service account authentication failure', error); throw new Error('Service account authentication failure'); } } async addEmailToGroup (email: string, groupKey: string, role: 'OWNER' | 'MEMBER' = 'MEMBER', syncLogger = logger) { if (!this.enabled) { logger.warn('Google Group service is disabled'); return; } syncLogger.debug(`Adding ${email} to ${groupKey}, with role ${role}`); const adminApi = await this.getAdminApi(); await adminApi.members.insert({ groupKey, requestBody: { email, role, type: 'USER' } }); } }If you have an example to share, illustrating how to go about it, that would be appreciated.
