使用Google Directory API时无法创建别名

时间:2018-10-24 15:52:33

标签: python api google-api gsuite google-directory-api

我的代码如下所示。我的问题是,当我尝试更新“别名”字段时,它不会发生。我的实现有什么问题?

store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('admin', 'directory_v1', http=creds.authorize(Http()))

body = { "name":{"familyName": "Aaab", "givenName": "Aaab"}, "password": "test@test", "primaryEmail": "testAAb@aaaa.no", "secondaryEmail": "test@gmail.com", 'aliases': ['testLeader@test.com']}
user_add = service.users().insert(body=body).execute()

使用其字段创建的用户:

    {'kind': 'admin#directory#user', 'id': '106377021897584806221', 'etag': '"TN30oD80QTVK45AAxvl_wbzs4vs/4WNsaqcVI4y7ARsciDEXH7K8Hh4"', 'primaryEmail': 'testaab@test.no', 'name': {'givenName': 'Aaab', 'familyName': 'Aaab', 'fullName': 'Aaab Aaab'}, 'isAdmin': False, 'isDelegatedAdmin': False, 'lastLoginTime': '1970-01-01T00:00:00.000Z', 'creationTime': '2018-10-24T16:43:27.000Z', 'agreedToTerms': True, 'suspended': False, 'archived': False, 'changePasswordAtNextLogin': False, 'ipWhitelisted': False, 'emails': [{'address': 'testaab@test.no', 'primary': True}], 'customerId': 'C02dcimb3', 'orgUnitPath': '/', 'isMailboxSetup': True, 'isEnrolledIn2Sv': False, 'isEnforcedIn2Sv': False, 'includeInGlobalAddressList': True}

1 个答案:

答案 0 :(得分:0)

问题似乎是您无法在Google Directory API中创建用户时为其指定别名。文档列出了here,因此aliases没有任何字段。

要向用户添加别名,似乎为此使用了不同的API调用。您必须使用here列出的alias.insert API调用手动插入所需的每个别名。似乎您所需要的只是唯一的userKey作为要传递给用户的字段以及别名。

此API调用的确切Python文档在here中列出。

使用此功能的示例代码:

body = {"alias": "testLeader@test.com"}
service.users().aliases().insert(userKey=key, body=body).execute()