Google 管理员 API pageToken 循环帮助 python

Google Admin API pageToken loop help python

我正在尝试检索公司中每个组的所有成员。我的代码工作正常,除非一个组中有超过 200 名成员,然后它会在实际列出成员之前输出几次组 ID,例如 "group2@company.com".

我不确定 pageToken 是如何工作的,因此无法为超过 200 名成员的组获取单个输出。再次感谢您对此提供的任何帮助。

此外,我添加了 del all_members[:] 以便它只显示该组的成员,否则它会附加上一个组的成员。

输出:

group1@company.com: 2 member(s)
user3@company.com - OWNER
user4@company.com - MEMBER

group2@company.com: 634 member(s)

group2@company.com: 634 member(s)

group2@company.com: 634 member(s)

group2@company.com: 634 member(s)
user1@company.com - MEMBER
user2@company.com - MEMBER
...

代码:

directory_service = authenticate()

all_groups = []
all_members = []
page_token = None
new_page_token = None
params = {'domain': 'company.com', 'customer': 'my_customer'}

while True:
  try:
    if page_token:
      params['pageToken'] = page_token
    current_page = directory_service.groups().list(**params).execute()

    all_groups.extend(current_page['groups'])
    page_token = current_page.get('nextPageToken')
    if not page_token:
      break
  except errors.HttpError as error:
    print 'An error occurred: %s' % error
    break

for group in all_groups:
  group_key = group['email']
  new_params = {'groupKey': '%s' % group_key, 'maxResults': '500'}

  if group['directMembersCount'] == '0':
    continue
  else:
    while True:
      try:
        if new_page_token:
          new_params['pageToken'] = new_page_token
        new_current_page = directory_service.members().list(**new_params).execute()

        print ''
        print '%s: %s member(s)' % (group['email'], group['directMembersCount'])

        all_members.extend(new_current_page['members'])
        new_page_token = new_current_page.get('nextPageToken')

        if not new_page_token:
          break
      except errors.HttpError as error:
        print 'An error occurred: %s' % error
        break

  for member in all_members:
    print '    %s - %s' % (member['email'], member['role'])
  del all_members[:]

将组打印语句移出 while 循环对我有用。我不确定为什么我以前没有看到它。希望这对其他人有帮助。

代码:

directory_service = authenticate()

all_groups = []
all_members = []
page_token = None
new_page_token = None
params = {'domain': 'company.com', 'customer': 'my_customer'}

while True:
  try:
    if page_token:
      params['pageToken'] = page_token
    current_page = directory_service.groups().list(**params).execute()

    all_groups.extend(current_page['groups'])
    page_token = current_page.get('nextPageToken')
    if not page_token:
      break
  except errors.HttpError as error:
    print 'An error occurred: %s' % error
    break

for group in all_groups:
  group_key = group['email']
  new_params = {'groupKey': '%s' % group_key, 'maxResults': '500'}

  if group['directMembersCount'] == '0':
    continue
  else:
    while True:
      try:
        if new_page_token:
          new_params['pageToken'] = new_page_token
        new_current_page = directory_service.members().list(**new_params).execute()
        all_members.extend(new_current_page['members'])
        new_page_token = new_current_page.get('nextPageToken')

        if not new_page_token:
          break
      except errors.HttpError as error:
        print 'An error occurred: %s' % error
        break

  print ''
  print '%s: %s member(s)' % (group['email'], group['directMembersCount'])

  for member in all_members:
    print '    %s - %s' % (member['email'], member['role'])
  del all_members[:]