Python 无法通过admin sdk插入子OU-父组织单元Id无效

Python 无法通过admin sdk插入子OU-父组织单元Id无效,python,google-admin-sdk,Python,Google Admin Sdk,环境详细信息 操作系统类型和版本:Fedora 32 Python版本:3.7.9 pip版本:PIP19.1.1 from/usr/lib/python3.7/site-packages/pip(python 3.7) googleapi python客户端version:1.12.5 复制步骤 { 'resp': { 'vary': 'Origin, X-Origin, Referer', 'content-type': 'application/

环境详细信息

  • 操作系统类型和版本:Fedora 32
  • Python版本:
    3.7.9
  • pip版本:
    PIP19.1.1 from/usr/lib/python3.7/site-packages/pip(python 3.7)
  • googleapi python客户端
    version:
    1.12.5
复制步骤

{
    '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': ''
}
  • 尝试使用orgunits.insert方法添加任何子OU
  • 代码示例

    {
        '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': ''
    }
    
    #self.orgunits是先前创建的,指向有效的google api会话。
    self.orgunits=build('admin','directory_v1',credentials=creds).orgunits()
    user.dept=“技术”
    user.func_unit=“系统”
    #这将返回一个看起来有效的值
    parent_id=self.orgunits.get(customerId=“xxxxxxx”,orgUnitPath=f“{user.dept}”).execute()['orgUnitId']
    #部门上级组织单元存在
    打印(self.orgunits.get(customerId=“xxxxxx”,orgUnitPath=f“{user.dept}”).execute())
    新用户={
    'name':f“{user.func_unit}”,
    “parentOrgUnitPath”:f“{user.dept}”
    }
    print(json.dumps(new#ou))#这里一切正常
    尝试:
    self.orgunits.insert(customerId=“xxxxxx”,body=json.dumps(new_ou)).execute()
    除了HttpError作为ie:
    印刷品
    
    输出

    {'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': ''}
    
    
    格式化的http响应

    {
        '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': ''
    }
    
    我尝试过的

    我参考了以下文件: 谷歌文档:

    Python文档:

    以下所有情况都会导致相同的
    400
    错误,并显示消息
    无效的父Orgunit Id

    • 我已尝试使用
      parentOrgUnitId
      而不是上述文档中概述的
      new\u ou
      对象中的
      parentOrgUnitPath
    • 我已尝试为
      parentorganitpath
      值添加/删除前导斜杠和尾随斜杠
    • 我已删除
      新对象中的fstring
    • 我试图复制另一个功能性OU的JSON。删除
      etag
      ,并用更新的值替换其余的值

    非常感谢您的帮助!谢谢大家!

    已解决

    在Google的OAuth2平台上进行了大量的跟踪和挖掘之后,我推断这是因为当为orgunit设置body参数时,insert不能用
    json.dumps
    格式化。它必须是Python字典

    更改:
    self.orgunits.insert(customerId=“xxxxxx”,body=json.dumps(new\u ou)).execute()

    致:
    self.orgunits.insert(customerId=“xxxxxx”,body=new\u ou).execute()

    您得到了什么结果?上面列出了输出。第三行是HTTP响应:
    “code”:400,\n“消息”:“无效的父Orgunit Id”
    。如果我使用已确认的父项Id(
    parent\u Id
    ),则返回正确的值,我收到相同的错误消息。