有关AdminDirectory.Orgunits.update方法的问题

时间:2019-06-18 21:17:28

标签: google-apps-script

我花了大部分时间试图找出正确的JSON正文和参数来更新OU;我实际上是在尝试重命名OU。我已经接近了,但是无论如何,到目前为止,解决方案都逃不过我了。

到目前为止,我已经引用了以下文档:

我已经尝试过将参数传递给AdminDirectory.Orgunits.update的对象中的参数的一些变体。最终,没有示例,因此我不确定100%正确的参数是什么。

到目前为止,这也是我的测试功能:

function test_renameOU(){

  /* Args:
  customerId: string, Immutable ID of the G Suite account (required)
  orgUnitPath: string, Full path of the organizational unit or its ID (required) (repeated)
  body: object, The request body. (required)
  The object takes the form of: 
  { # JSON template for Org Unit resource in Directory API.
    "kind": "admin#directory#orgUnit", # Kind of resource this is.
    "parentOrgUnitPath": "A String", # Path of parent OrgUnit
    "name": "A String", # Name of OrgUnit
    "etag": "A String", # ETag of the resource.
    "orgUnitPath": "A String", # Path of OrgUnit
    "parentOrgUnitId": "A String", # Id of parent OrgUnit
    "blockInheritance": True or False, # Should block inheritance
    "orgUnitId": "A String", # Id of OrgUnit
    "description": "A String", # Description of OrgUnit
  }
  */

  /* Function to perform rename */
  function renameOU(customerId, orgUnitPath, body){
    Logger.log(customerId + ", " + orgUnitPath + ", " + JSON.stringify(body))
    try{
      var org = AdminDirectory.Orgunits.update(customerId, orgUnitPath, body)
      }catch(e){
        Logger.log(JSON.stringify(e));
      }
  }

  /* Arguments */ 
  var customerId = 'my_customer';
  var oldOUname = "Education";
  var parentOrgUnitPath = "/Users/200 COGS";
  var orgUnitId = "id:03ph8a2z39wdr3v";
  var orgUnitPath = parentOrgUnitPath + "/" + oldOUname;
  var parentOrgUnitId = "id:03ph8a2z1lakohp";
  var newOUname = "255 Education";
  Logger.log(orgUnitPath);
  var body = { //# JSON template for Org Unit resource in Directory API.
    "kind": "admin#directory#orgUnit", //# Kind of resource this is.
    "parentOrgUnitPath": parentOrgUnitPath, //# Path of parent OrgUnit
    "name": newOUname, //# Name of OrgUnit
    "orgUnitPath": parentOrgUnitPath + "/" + newOUname, //# Path of OrgUnit
    "parentOrgUnitId": parentOrgUnitId, //# Id of parent OrgUnit
    "blockInheritance": false, //# Should block inheritance
    "orgUnitId": orgUnitId, //# Id of OrgUnit
  }

  /* Call Rename Function */
  Logger.log(customerId + ", " + orgUnitId + ", " + JSON.stringify(body))
  renameOU(customerId, orgUnitId, body)
}

我希望OU的结果从“ / Users / 200 COGS / Education”更改为“ / Users / 200 COGS / 255 Education”。

输出是解析错误:

[19-06-17 17:39:39:165 PDT] /Users/200 COGS/Education
[19-06-17 17:39:39:166 PDT] my_customer, id:03ph8a2z39wdr3v, {"kind":"admin#directory#orgUnit","parentOrgUnitPath":"/Users/200 COGS","name":"255 Education","orgUnitPath":"/Users/200 COGS/255 Education","parentOrgUnitId":"id:03ph8a2z1lakohp","blockInheritance":false,"orgUnitId":"id:03ph8a2z39wdr3v"}
[19-06-17 17:39:39:166 PDT] my_customer, id:03ph8a2z39wdr3v, {"kind":"admin#directory#orgUnit","parentOrgUnitPath":"/Users/200 COGS","name":"255 Education","orgUnitPath":"/Users/200 COGS/255 Education","parentOrgUnitId":"id:03ph8a2z1lakohp","blockInheritance":false,"orgUnitId":"id:03ph8a2z39wdr3v"}
[19-06-17 17:39:39:198 PDT] {"message":"API call to directory.orgunits.update failed with error: Parse Error","name":"GoogleJsonResponseException","fileName":"GSuiteOrgUnits","lineNumber":573,"stack":"\tat GSuiteOrgUnits:573 (renameOU)\n\tat GSuiteOrgUnits:600 (test_renameOU)\n","details":{"message":"Parse Error","code":400,"errors":[{"domain":"global","reason":"parseError","message":"Parse Error"}]}}

2 个答案:

答案 0 :(得分:0)

如果您使用patch端点,则只需传递要更改的字段:

AdminDirectory.Orgunits.patch({
  name: 'New Name'
}, customerId, ouIdOrPath);

答案 1 :(得分:0)

它对我有用。不要传递单元路径组织,因为它是由 api 根据“名称”字段自动调整的

var orgUnit = new OrgUnit()
{
Name = "new name",
Description = "new description"
};
var request = service.Orgunits.Patch(orgUnit, customerId, orgUnitPath);
request.Execute();