1
votes

I am not able to patch/update custom fields for my users via Google Directory API in Python. I am using google-api-python-client/1.5.1.

I am able to patch/update custom fields via https://developers.google.com/admin-sdk/directory/v1/reference/users/patch with json generated by my script. I am also able to patch using curl with json generated by my script. However when I try to patch/update directly from my script, no change is made. It is strange, as I am successful in patching other fields (mobile number, landline etc.) from the same script with the same syntax.

...
OAUTH_SCOPE = 'https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.group'

credentials = SignedJwtAssertionCredentials(
'service-account-email',
key,
scope=OAUTH_SCOPE,
sub='[email protected]')
httplib2.debuglevel = 1
http = httplib2.Http()
http = credentials.authorize(http)
directory_service = build('admin', 'directory_v1', http=http)    
params = {'customer': 'my_customer'}
...
Get user info from LDAP, returns mail, mobile and groups
...
patch = {'phones':
 [
  {'value': mobile, 'type': 'work_mobile', 'primary': 'true'}
 ]
}
try:
 patchr = directory_service.users().patch(userKey=mail, body=patch).execute(http=http)
except errors.HttpError as e:
 print e
customSchemas['aswSchema']['adGroups'] = []
for group in groups:
 customSchemas['aswSchema']['adGroups'].append({'value': group})
patchg = json.dumps(customSchemas)
try:
 patchr = directory_service.users().patch(userKey=mail, body=patchg).execute(http=http)
except errors.HttpError as e:
 print e    
....

patchg variable is for example following:

{"aswSchema":
 {"adGroups":
 [
  {"value": "r_app_app1-ro"},
 ]
 }
}

I see this in the debug log of httplib2:

send: 'PATCH /admin/directory/v1/users/user%40example.net?alt=json HTTP/1.1\r\nHost: www.googleapis.com\r\ncontent-length: 69\r\naccept-encoding: gzip, deflate\r\naccept: application/json\r\nuser-agent: google-api-python-client/1.5.1 (gzip)\r\ncontent-type: application/json\r\nauthorization: Bearer very-long-string\r\n\r\n"{\\"aswSchema\\": {\\"adGroups\\": [{\\"value\\": \\"r_net_app1-ro\\"}]}}"'
reply: 'HTTP/1.1 200 OK\r\n'

But user is not updated with adGroups fields.

What could be the problem?

1

1 Answers

0
votes

Try sending just customSchemas instead of patchg.

I ran into something similar where I shouldn't have dumped to a string.

In my current code where I change customSchema, I'm currently just sending the patch dict in the submission body and that seems to work fine.