0

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.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117

1 Answers1

0

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.

Jay Lee
  • 13,415
  • 3
  • 28
  • 59
  • Thanks Jay. Appreciate it, Will try this. – user1816974 Mar 14 '15 at 20:34
  • I am interested only in the group names from this list. How do I retrieve just the groups? – user1816974 Mar 31 '15 at 16:32
  • @user1816974 the group names are part of the dictionaries in `items`. Just do `all_groups += [item['name'] for item in items]`. See https://developers.google.com/admin-sdk/directory/v1/guides/manage-groups#get_all_member_groups – LeoRochael Jul 12 '18 at 18:25