Python 2.7 目录api批检索域中所有用户的python代码

Python 2.7 目录api批检索域中所有用户的python代码,python-2.7,google-admin-sdk,google-directory-api,Python 2.7,Google Admin Sdk,Google Directory Api,目前,我有一个方法可以检索所有~119000个gmail帐户,并使用下面的python代码和启用的admin.sdk+auth 2.0将它们写入csv文件: def get_accounts(self): students = [] page_token = None params = {'customer': 'my_customer'} while True: try: if page_token:

目前,我有一个方法可以检索所有~119000个gmail帐户,并使用下面的python代码和启用的admin.sdk+auth 2.0将它们写入csv文件:

def get_accounts(self):
    students = []
    page_token = None
    params = {'customer': 'my_customer'}

    while True:
        try:
            if page_token:
                params['pageToken'] = page_token
            current_page = self.dir_api.users().list(**params).execute()

            students.extend(current_page['users'])

            # write each page of data to a file
            csv_file = CSVWriter(students, self.output_file)
            csv_file.write_file()

            # clear the list for the next page of data
            del students[:]

            page_token = current_page.get('nextPageToken')

            if not page_token:
                break

        except errors.HttpError as error:
            break

我想一次性检索所有119000,也就是说,不必循环或批处理调用。这可能吗?如果可能的话,您能提供示例python代码吗?我遇到了通信问题,必须多次重新运行该过程才能成功获得~119000个帐户,下载大约需要10分钟。希望尽量减少通信错误。请告知是否存在更好的方法,或者是否也可以使用非循环方法

无法将此作为一个批处理,因为您需要知道每个pageToken,而这些标记仅在检索页面时提供。但是,通过获得更大的页面,您可以在一定程度上提高性能:

params = {'customer': 'my_customer', 'maxResults': 500}
由于未设置maxResults时的默认页面大小为100,因此添加maxResults:500将使API调用数减少5个数量级。虽然每个调用可能需要稍长的时间,但您应该注意到性能的提高,因为您进行的API调用和HTTP往返要少得多

您还应该考虑使用fields参数仅指定需要在列表中读取的用户属性。这样,您就不会浪费时间和带宽检索应用程序从未使用过的用户详细信息。尝试以下方法:

my_fields = 'nextPageToken,users(primaryEmail,name,suspended)'
params = {
  'customer': 'my_customer',
   maxResults': 500,
   fields: my_fields
   }

最后,如果您的应用程序相当频繁地检索用户列表,可能会有所帮助。

响应非常好!正是我想要的。我将在下一次更新中实施修改,希望这将解决我们偶尔遇到的超时/通信问题。非常感谢。