Question

I am trying to obtain the number of groups per user domain. So I first get the user domains from the Directory APi.Users and the primaryEmail field and then obtain the number of groups per domain.

The first part of collecting domains gets the right results. The second part raises <class 'apiclient.errors.HttpError'> with an exception.message = "group" (not very useful indeed). The exception is only raised for some of the domains and not all.

Here's a snippet:

def groupsPerDomain():
    #code that gets domains... omitted for brevity
    #domain_stats = {domainName: <some group info>,...}
    #service directory is a directory service instance
    groups_sub = service_directory.groups()
    for domain in domain_stats:
        request = groups_sub.list(domain=domain, 
              fields="groups(directMembersCount,id,name)")
        domain_groups_count = 0
        while request != None:
            groups_page = request.execute() #THE EXCEPTION IS RAISED HERE
            #count groups per domain
            domain_groups_count += len(groups_page["groups"]) 
            request = groups_sub.list_next(request, groups_page)
        #save counted groups per domain
        domain_stats[domain]["group_count"] = domain_groups_count

    return domain_stats

Thanks.

Was it helpful?

Solution

If you are going to use pagination you have to include nextPageToken as one of the fields.

request = groups_sub.list(domain=domain,fields="nextPageToken,groups(directMembersCount,id,name)")

I'd suggest instead of making one API call for each domain, getting all the groups for the account and then parsing the address to know which domain they belong to. Don't forget that groups can also have aliases

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top