问题描述
环境详细信息
- 操作系统类型和版本:Fedora 32
- Python版本:
3.7.9
- 点子版本:
pip 19.1.1 from /usr/lib/python3.7/site-packages/pip (python 3.7)
-
google-api-python-client
版本:1.12.5
复制步骤
代码示例
#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__)
{'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,'-content-encoding': 'gzip'
},'content': b '{\n "error": {\n "code": 400,'error_details': ''
}
我尝试过的事情
我正在参考以下文档: Google文件:https://developers.google.com/admin-sdk/directory/v1/reference/orgunits/insert
以下所有结果导致与消息400
相同的Invalid Parent Orgunit Id
错误。
- 如上所述,我尝试在
parentOrgUnitId
对象中使用parentOrgUnitPath
代替new_ou
。 - 我尝试为
parentOrgUnitPath
值添加/删除前斜杠和后斜杠。 - 我已删除了
new_ou
对象内部的字符串。 - 我试图复制另一个功能性OU的JSON。删除了
etag
并将其余的值替换为更新的值。
任何帮助将不胜感激!谢谢!
解决方法
已解决
在Google的OAuth2游乐场中进行了大量的跟踪和挖掘之后,我推断出这是因为,当为orgunit设置body参数时,将其插入不能格式化为json.dumps
。 必须是Python字典。
更改:
self.orgunits.insert(customerId="xxxxxx",body=json.dumps(new_ou)).execute()
收件人:
self.orgunits.insert(customerId="xxxxxx",body=new_ou).execute()