1

我一直在使用 Directory API 列出用户,列出的用户数量有些不一致。

我能够使用检索用户的信息

admin_service.users().get(userKey = email).execute()

但是当从列表 API 检索时该用户不存在时

credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL,PRIVATE_KEY,SCOPE,prn = "admin@domain.com")

http = httplib2.Http()
http = credentials.authorize(http)

admin_service = build("admin", "directory_v1",http = http)
admin_service.users()

user_list = user_service.list(domain="company.com")
listed_users = user_list.execute()

final_user_list = listed_users["users"]
while "nextPageToken" in listed_users.keys():
    user_list = user_service.list(pageToken = listed_users["nextPageToken"])
    listed_users = user_list.execute()
    final_user_list += listed_users["users"]

return final_user_list

例如,如果列表 API 返回 2000 个用户,并且列表中缺少用户 john.doe@domain.com。

从 API 查询 john.doe@domain.com 的详细信息时,我们得到了成功的结果。

4

1 回答 1

0

尝试这个

credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL,PRIVATE_KEY,SCOPE,prn = "admin@domain.com")
http = httplib2.Http()
http = credentials.authorize(http)

service = build("admin", "directory_v1",http = http)
final_user_list = []

page_token = None
while True:
    users = service.users().list(domain="replace_your_domain_name", pageToken=page_token).execute()
    for entry in users.get("users"):
        user_email = entry.get('primaryEmail')
        final_user_list.append(user_email)
    page_token = users.get('nextPageToken', None)
    if not page_token:
        break
于 2014-04-12T09:08:37.430 回答