Python Retrieving all Groups from a domain using OAuth2

607 Views Asked by At

Using PYTHON, To get all groups in a domain, in OAuth1 had a command like:

groupfeed = api(lambda: GROUPS_SERVICE.RetrieveAllGroups()) 

In OAuth2, will it be

allGrps = client.groups().list(customer='my_company').execute()

I am looking for the equivalent code to get ALL groups in a domain. Thanks for your help and attention.

1

There are 1 best solutions below

3
Jay Lee On

If you have more than 200 groups, the results will be paged and returned over multiple API calls. You need to keep retrieving pages until none are left:

all_groups = []
request = client.groups().list(customer='my_customer')
while True: # loop until no nextPageToken
  this_page = request.execute()
  if 'items' in this_page:
    all_groups += this_page['items']
  if 'nextPageToken' in this_page:
    request = client.groups().list(
      customer='my_customer',
      pageToken=this_page['nextPageToken'])
  else:
    break

also notice that it's my_customer, not my_company.