Skip to content

Instantly share code, notes, and snippets.

@ptflp
Created July 6, 2018 23:55
Show Gist options
  • Save ptflp/052d0601cb09e43fc78de80789ba8930 to your computer and use it in GitHub Desktop.
Save ptflp/052d0601cb09e43fc78de80789ba8930 to your computer and use it in GitHub Desktop.
nodejs googleapis example
const google = require('googleapis');
const KEY = require('./mykey.json');
getAllUsers();
/* CREATE CONNECTION, get instance of jwtClient */
function connect() {
return new Promise((yep, nope) => {
const jwtClient = new google.auth.JWT(
KEY.client_email,
null,
KEY.private_key,
['https://www.googleapis.com/auth/admin.directory.user'],
'adminuser@mydomain.com'
);
jwtClient.authorize((err) => {
if(err) {
nope(err);
} else {
yep(jwtClient);
}
});
});
}
/* List first 100 users, client is jwtClient instance */
function listUsers(client) {
return new Promise((yep, nope) => {
google.admin('directory_v1').users.list({
auth: client,
domain: 'mydomain.com',
}, (err, response) => {
if (err) {
return nope(err);
}
return yep(response);
});
});
}
/* Get all users using asynchronous code with promise */
function getAllUsers() {
return connect().then((client) => (listUsers(client)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment