Skip to content

Instantly share code, notes, and snippets.

@DazWilkin
Created March 27, 2019 00:45
Show Gist options
  • Save DazWilkin/dca8c3db8879765632d4c4be8d662074 to your computer and use it in GitHub Desktop.
Save DazWilkin/dca8c3db8879765632d4c4be8d662074 to your computer and use it in GitHub Desktop.
Stackoverflow #55345991
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SERVICE_ACCOUNT_EMAIL = '[[ROBOT]]@[[PROJECT]].iam.gserviceaccount.com'
SERVICE_ACCOUNT_FILE_PATH = './credentials.json'
USER_EMAIL = 'user@domain.com'
def main():
credentials = ServiceAccountCredentials.from_json_keyfile_name(
SERVICE_ACCOUNT_FILE_PATH,
scopes=['https://www.googleapis.com/auth/admin.directory.user']
)
credentials = credentials.create_delegated(USER_EMAIL)
service = build('admin', 'directory_v1', credentials=credentials)
print('Getting the first 10 users in the domain')
results = service.users().list(customer='my_customer', maxResults=10,
orderBy='email').execute()
users = results.get('users', [])
if not users:
print('No users in the domain.')
else:
print('Users:')
for user in users:
print(u'{0} ({1})'.format(user['primaryEmail'],
user['name']['fullName']))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment