0

Environment details

  • OS type and version: Fedora 32
  • Python version: 3.7.9
  • pip version: pip 19.1.1 from /usr/lib/python3.7/site-packages/pip (python 3.7)
  • google-api-python-client version: 1.12.5

Steps to reproduce

  1. Attempt to add any sub-OU with the orgunits.insert method

Code example

#self.orgunits is created previously and points to a valid google api session.
self.orgunits = build('admin', 'directory_v1', credentials=creds).orgunits()
user.dept = "Technology"
user.func_unit = "Systems"

#This returns a value that appears valid
parent_id = self.orgunits.get(customerId="xxxxxxx", orgUnitPath=f"{user.dept}").execute()['orgUnitId']

#The department parent org unit exists
print(self.orgunits.get(customerId="xxxxxx", orgUnitPath=f"{user.dept}").execute())
new_ou = {
            'name': f"{user.func_unit}",
            'parentOrgUnitPath' : f"{user.dept}"
            }
print(json.dumps(new_ou)) #Everything appears fine here
try:
    self.orgunits.insert(customerId="xxxxxx", body=json.dumps(new_ou)).execute()
except HttpError as ie:
    print(ie.__dict__)

Output

{'kind': 'admin#directory#orgUnit', 'etag': '"HKDSgTnCxrWl3RtRnlZSCPY3NjdWJxz53nrhwSz7ob4/w1e7lJjaci7tVElbAz8hlgavRvg"', 'name': 'Technology', 'description': 'Technology department', 'orgUnitPath': '/Technology', 'orgUnitId': 'id:03ph8a2z0jkzi9y', 'parentOrgUnitPath': '/', 'parentOrgUnitId': 'id:037oov482lkthdl'}
{"name": "Systems", "parentOrgUnitPath": "/Technology"}
{'resp': {'vary': 'Origin, X-Origin, Referer', 'content-type': 'application/json; charset=UTF-8', 'date': 'Wed, 28 Oct 2020 20:59:43 GMT', 'server': 'ESF', 'cache-control': 'private', 'x-xss-protection': '0', 'x-frame-options': 'SAMEORIGIN', 'x-content-type-options': 'nosniff', 'alt-svc': 'h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"', 'transfer-encoding': 'chunked', 'status': '400', 'content-length': '224', '-content-encoding': 'gzip'}, 'content': b'{\n  "error": {\n    "code": 400,\n    "message": "Invalid Parent Orgunit Id",\n    "errors": [\n      {\n        "message": "Invalid Parent Orgunit Id",\n        "domain": "global",\n        "reason": "invalid"\n      }\n    ]\n  }\n}\n', 'uri': 'https://www.googleapis.com/admin/directory/v1/customer/xxxxxx/orgunits?alt=json', 'error_details': ''}

Formatted http response

{
    'resp': {
        'vary': 'Origin, X-Origin, Referer',
        'content-type': 'application/json; charset=UTF-8',
        'date': 'Wed, 28 Oct 2020 20:59:43 GMT',
        'server': 'ESF',
        'cache-control': 'private',
        'x-xss-protection': '0',
        'x-frame-options': 'SAMEORIGIN',
        'x-content-type-options': 'nosniff',
        'alt-svc': 'h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
        'transfer-encoding': 'chunked',
        'status': '400',
        'content-length': '224',
        '-content-encoding': 'gzip'
    },
    'content': b '{\n  "error": {\n    "code": 400,\n    "message": "Invalid Parent Orgunit Id",\n    "errors": [\n      {\n        "message": "Invalid Parent Orgunit Id",\n        "domain": "global",\n        "reason": "invalid"\n      }\n    ]\n  }\n}\n',
    'uri': 'https://www.googleapis.com/admin/directory/v1/customer/xxxxxx/orgunits?alt=json',
    'error_details': ''
}

What I have Tried

I am referencing the following documentation: Google docs: https://developers.google.com/admin-sdk/directory/v1/reference/orgunits/insert

Python docs: https://developers.google.com/resources/api-libraries/documentation/admin/directory_v1/python/latest/admin_directory_v1.orgunits.html#insert

All of the following results in the same 400 error with the message Invalid Parent Orgunit Id .

  • I have tried using parentOrgUnitId instead of parentOrgUnitPath in the new_ou object as outlined in the above documentation.
  • I have tried adding/removing leading and trailing slashes for the parentOrgUnitPath value.
  • I have removed fstrings inside of new_ou object.
  • I have tried to copy the JSON of another, functional OU. Removed etag and and replaced the rest of the values with updated values.

Any help is greatly appreciated! Thank you!

  • What result do you get? – Jescanellas Oct 30 '20 at 10:01
  • The output is listed above. The third line is the HTTP response: `"code": 400,\n "message": "Invalid Parent Orgunit Id"`. This is false as if I use the Id of the parent (`parent_id`) which I have confirmed returns the proper value I receive the same error message. – Samuel Coles Oct 30 '20 at 14:28

1 Answers1

0

Solved

After much tracing and digging in Google's OAuth2 playground I have deduced that this is because when the body argument is set for the orgunit insert it cannot be formatted with json.dumps. It must be a Python dictionary.

Changing: self.orgunits.insert(customerId="xxxxxx", body=json.dumps(new_ou)).execute()

To: self.orgunits.insert(customerId="xxxxxx", body=new_ou).execute()