Created
July 16, 2020 15:03
-
-
Save camerondavison/592e3ace8599834f43f767b8c0dfd715 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env python3 | |
| from googleapiclient.discovery import build | |
| from oauth2client.service_account import ServiceAccountCredentials | |
| import structlog | |
| log = structlog.get_logger() | |
| def main(): | |
| SERVICE_ACCOUNT_FILE = "credentials.json" | |
| SCOPES = [ | |
| "https://www.googleapis.com/auth/admin.directory.user.readonly", | |
| "https://www.googleapis.com/auth/admin.directory.group.readonly", | |
| ] | |
| delegated_user = "[email protected]" | |
| credentials = ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT_FILE, scopes=SCOPES) | |
| credentials = credentials.create_delegated(delegated_user) | |
| service = build("admin", "directory_v1", credentials=credentials) | |
| found_groups = [] | |
| n = 0 | |
| request = None | |
| results = None | |
| while True: | |
| if request is None: | |
| request = service.groups().list(customer="my_customer", orderBy="email",) | |
| else: | |
| request = service.groups().list_next(request, results) | |
| if not request: | |
| break | |
| results = request.execute() | |
| groups = results.get("groups", []) | |
| for group in groups: | |
| n += 1 | |
| if n % 50 == 0: | |
| log.info(f"through {n} and found {len(found_groups)}") | |
| member_count = int(group["directMembersCount"]) | |
| if member_count > 0: | |
| found_groups.append(group["email"]) | |
| log.info(f"found {n} groups where {len(found_groups)} have members") | |
| if __name__ == "__main__": | |
| main() |
Author
camerondavison
commented
Jul 16, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment