Admin SDK中的用户列表不一致

时间:2014-04-11 11:05:25

标签: google-oauth google-admin-sdk google-directory-api

我一直在列出使用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

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

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

1 个答案:

答案 0 :(得分: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