Python script (or any other language) for listing membership of groups and subgroups


We may need to restructure our groups/subgroups to limit user access to a need-to-know basis.

I was wondering if anyone had tried this before and what pitfalls they might have encountered.

Also, has anyone created a python script or other that lists membership of all groups and subgroups? I got started by listing members of the top-level group with the script below, but I’m not clear how to how to proceed to the next step of retrieving the subgroups and their memberships.

Python script to list members of top group:

import requests
import csv

API endpoint and token

url = “
headers = {
“Authorization”: “Bearer xxxxxxxxxxxxxxxxxx”

Send GET request to API

response = requests.get(url, headers=headers)
data = response.json()

Filter out the relevant fields

filtered_data =
for member in data:
“id”: member[“id”],
“name”: member[“name”],
“username”: member[“username”]

Print the desired information horizontally

print(“id”.ljust(10), “name”.ljust(30), “username”)
print(“=” * 60)
for member in filtered_data:
print(str(member[“id”]).ljust(10), member[“name”].ljust(30), member[“username”])

Write the data to a CSV file

csv_filename = “members_list.csv”
with open(csv_filename, “w”, newline=“”) as csv_file:
fieldnames = [“id”, “name”, “username”]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

print(“\nData written to”, csv_filename)
print(“Total members:”, len(filtered_data))