Skip to content

Instantly share code, notes, and snippets.

@heyseus1
Created November 22, 2019 23:59
Show Gist options
  • Save heyseus1/7c04e4b5707194d4ee4e09fdcbe66aa1 to your computer and use it in GitHub Desktop.
Save heyseus1/7c04e4b5707194d4ee4e09fdcbe66aa1 to your computer and use it in GitHub Desktop.
pulls a list of users from a google distro list
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import requests
import sys
### command line args: python google-group-list-users.py [email protected]
groupid = sys.argv[1]
groupid = '{}'.format(groupid)
SCOPES = ['https://www.googleapis.com/auth/admin.directory.group']
def main():
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('admin', 'directory_v1', credentials=creds)
results = service.members().list(groupKey=groupid, maxResults=200).execute()
members = results.get('members')
for member in members:
print(member.get('email'))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment