title: Patch/update custom fields for users via Google Directory API in Python
body: <p>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.</p>
  
  <p>I am able to patch/update custom fields via <a href="https://developers.google.com/admin-sdk/directory/v1/reference/users/patch" rel="nofollow">https://developers.google.com/admin-sdk/directory/v1/reference/users/patch</a> 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.</p>
  
  <pre><code>...
  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='admin@example.net')
  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    
  ....
  </code></pre>
  
  <p>patchg variable is for example following:</p>
  
  <pre><code>{"aswSchema":
   {"adGroups":
   [
    {"value": "r_app_app1-ro"},
   ]
   }
  }
  </code></pre>
  
  <p>I see this in the debug log of httplib2:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>But user is not updated with adGroups fields.</p>
  
  <p>What could be the problem?</p>