Created
July 6, 2018 23:55
-
-
Save ptflp/052d0601cb09e43fc78de80789ba8930 to your computer and use it in GitHub Desktop.
nodejs googleapis example
This file contains hidden or 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
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'], | |
'[email protected]' | |
); | |
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