The code I have is shown below. My problem is that when I am trying to update the 'aliases'-field it will not happen. What is wrong with my implementation?
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()
The user that is created with it's fields:
{'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}
The problem seems to be that you cannot specify an alias for a user when creating them in the Google Directory API. As the documentation lists here there is no field for aliases
.
To add an alias to a user there seems to be a different API call used for that. You have to manually insert each alias you want using the alias.insert
API call as listed here. It seems all you need is the unique userKey
as the field to pass in along with alias for the user.
The exact Python documentation for this API call is listed here.
Example code using this function:
body = {"alias": "testLeader@test.com"}
service.users().aliases().insert(userKey=key, body=body).execute()