Google Admin API pageToken循环帮助python

时间:2015-10-09 17:33:50

标签: python google-admin-sdk google-api-python-client google-directory-api

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

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

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

输出:

  

group1@company.com:2名成员
  user3@company.com - 所有者
  user4@company.com - 会员

     

group2@company.com: 634名成员

     

group2@company.com: 634名成员

     

group2@company.com: 634名成员

     

group2@company.com:634名成员
  user1@company.com - 会员
  user2@company.com - 会员
  ...

代码:

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[:]

1 个答案:

答案 0 :(得分:0)

在while循环之外移动group print语句对我有用。我不确定为什么我之前没有看到它。希望这有助于其他人。

代码:

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[:]