Skip to content

Instantly share code, notes, and snippets.

@jason-dark
Last active August 26, 2021 09:21
Show Gist options
  • Save jason-dark/d53cbfcac26aa937b2d9798f85790663 to your computer and use it in GitHub Desktop.
Save jason-dark/d53cbfcac26aa937b2d9798f85790663 to your computer and use it in GitHub Desktop.
const { google } = require('googleapis');
const privatekey = require('./private-key.json'); // This was downloaded when you created your Service Account Key
const scopes = ['https://www.googleapis.com/auth/gmail.settings.basic'];
const auth = async (user) => {
try {
const jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
scopes,
user // User who will be impersonated with this JWT client
);
await jwtClient.authorize();
return jwtClient;
} catch (err) {
return err;
}
}
// Users who will be impersonated
const users = [
'user1@email.com',
'user2@email.com',
'user3@emai.com'
];
for (const user of users) {
auth(user).then((jwtClient) => {
// make request with JWT Client
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment