通过Appmaker

时间:2019-05-09 18:43:35

标签: google-apps-script google-app-maker

我正在AppMaker中创建新的员工/用户配置工作流程。我正在创建一个新的G Suite用户,此时我的JSON中存在一个非常奇怪的问题。导致错误:GoogleJsonResponseException:对directory.users.insert的API调用失败,错误为:

  

provisionUser(AdminDirectory:5)输入无效

我已经对此进行了一段时间的故障排除,并且似乎在AppMaker和AdminDirectory调用之间的某个地方,一些额外的空格和硬返回被插入到我的用户数据中。下面我将展示:

  1. 用于收集用户信息的客户端代码
  2. 服务器端用户创建脚本
  3. 用户JSON的控制台输出
  4. 我的电子邮件中用于故障排除的用户JSON的输出
  5. 在记事本++中看到的通过电子邮件发送的输出

客户端代码

...a bunch of data gathering from a form, then...
  var user = {
    primaryEmail: email,
    name: {
      givenName: firstName,
      familyName: lastName
    },
    addresses: [{
      type: 'work',
      formatted: address
    }],
    organizations: [{
      title: title,
      department: department,
      fullTimeEquivalent: ftpt
    }],
    phones: [{
      type: 'work',
      value: phone
    }],
    locations: [{
      buildingId: building,
      type: 'desk',
      area: 'desk'
    }],
    password: 'xxxxxxxx',
    changePasswordAtNextLogin: true,
    orgUnitPath: orgUnit,
    relations: [{
      type: 'manager',
      value: supervisor
    }],
    customSchemas: {
      sclsnj: {
        startDate: effective,
        mls: mls,
        location: location
      }
    }
  };
  google.script.run.provisionUser(user, grouparray);

服务器端代码

function provisionUser(user, grouparray) {
  user = JSON.stringify(user);
  MailApp.sendEmail('lhoffman@xxxxxxxx.org', 'New Google User', user); 
  console.log(user);
  user = AdminDirectory.Users.insert(user);
  for (var g = 0; g < grouparray.length; g++) {
    var groupEmail = grouparray[g] + '@xxxxxxxx.org';
    var member = {
      email: user,
      role: 'MEMBER'
    };
    Logger.log(groupEmail);
    AdminDirectory.Members.insert(member, groupEmail);
  }  
}

控制台输出

{"primaryEmail":"jsmith@xxxxxxxx.org","name":{"givenName":"John","familyName":"Smith"},"addresses":[{"type":"work","formatted":"Bound Brook branch\n402 E High Street, Bound Brook, NJ 08805"}],"organizations":[{"title":"Library Technician","department":"Adult Services","fullTimeEquivalent":100000}],"phones":[{"type":"work","value":"908-458-8410"}],"locations":[{"buildingId":"BBROOK","type":"desk","area":"desk"}],"password":"xxxxxxxx","changePasswordAtNextLogin":true,"orgUnitPath":"/Branches/Bound Brook branch","relations":[{"type":"manager","value":"msmith@xxxxxxxx.org"}],"customSchemas":{"sclsnj":{"startDate":"2019-05-20T04:00:00.000Z","mls":false,"location":"Bound Brook branch"}}}

电子邮件输出

{"primaryEmail":"jsmith@xxxxxxxx.org","name":{"givenName":"John","familyName":"Smith"},"addresses":[{"type":"work","formatted":"Bound  
Brook  
branch\n402 E High Street, Bound Brook, NJ  
08805"}],"organizations":[{"title":"Library Technician","department":"Adult  
Services","fullTimeEquivalent":100000}],"phones":[{"type":"work","value":"908-458-8410"}],"locations":[{"buildingId":"BBROOK","type":"desk","area":"desk"}],"password":"xxxxxxxx","changePasswordAtNextLogin":true,"orgUnitPath":"/Branches/Bound  
Brook  
branch","relations":[{"type":"manager","value":"msmith@xxxxxxxx.org"}],"customSchemas":{"sclsnj":{"startDate":"2019-05-20T04:00:00.000Z","mls":false,"location":"Bound  
Brook branch"}}}

Notepad ++中的电子邮件输出(带有控制字符)

Output with Control Characters.jpg

注意:我知道JSON实际上在图像中代表了两次,但是您肯定可以看到多余的空格,并在其中保留了提要字符。

我已经确认可以使用Google API资源管理器为用户提供控制台输出,并将其复制并粘贴到Directory API Users.insert的请求正文中。

我还确认,使用相同的方法通过电子邮件发送的输出版本起作用。当我将该版本粘贴到API资源管理器中的请求正文中时,会收到错误警报,并且从正文字段中的颜色编码中可以明显看出它是不正确的。即使我不通过电子邮件将输出发送给自己,也会出现相同的错误行为,因此,我很确定电子邮件的行为不会弄乱事情。

帮助!

2 个答案:

答案 0 :(得分:2)

无效输入是特定字段未正确格式化的结果。就您而言,我看到您正在使用自定义架构。自定义架构中有一个格式不正确的特定文件,即 startDate 。与其提供 2019-05-20T04:00:00.000Z 的值,不如使用 2019-05-20

之所以这样,是因为该字段期望的是仅完成日期值,而不是完成日期加上小时和分钟。正如您在ISO-8601 date format上看到的那样,完成日期格式为 YYYY-MM-DD

参考:https://developers.google.com/admin-sdk/directory/v1/reference/schemas/insert

答案 1 :(得分:0)

如果这样做,会发生什么?

function provisionUser(user, grouparray) {
  var user = JSON.stringify(user);
  MailApp.sendEmail('lhoffman@xxxxxxxx.org', 'New Google User', user); 
  console.log(user);
  AdminDirectory.Users.insert(user);
  for (var g = 0; g < grouparray.length; g++) {
    var groupEmail = grouparray[g] + '@xxxxxxxx.org';
    var member = {
      email: user,
      role: 'MEMBER'
    };
    Logger.log(groupEmail);
    AdminDirectory.Members.insert(member, groupEmail);
  }  
}