Search code examples
google-apps-scriptgoogle-app-maker

Added spaces and LRs in JSON via Appmaker


I'm working in AppMaker to create a new employee/user provisioning workflow. I'm at the point where I am creating a new G Suite user, and I have a very weird problem with spaces in my JSON. It's leading to an error: GoogleJsonResponseException: API call to directory.users.insert failed with error:

Invalid Input at provisionUser (AdminDirectory:5)

I've been troubleshooting this for a while, and it seems that somewhere between AppMaker and the AdminDirectory call, some extra spaces and hard returns are inserted into my user data. Below I'm going to show:

  1. The client-side code for gathering user information
  2. The server-side user creation script
  3. The console output of the user JSON
  4. The output of the user JSON that comes to my email for troubleshooting
  5. The emailed output as seen in Notepad++

Client-Side Code

...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);

Server-Side Code

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);
  }  
}

Console Output

{"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"}}}

Email Output

{"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"}}}

Email Output in Notepad++ (with control characters)

Output with Control Characters.jpg

NOTE: I realize the JSON is actually represented in the image twice, but you can definitely see the extra space and left feed characters there.

I've confirmed that I can provision a user with the console output by using the Google API Explorer and copying and pasting it into the request body for the Directory API Users.insert.

I've also confirmed that the emailed version of the output does not work using the same method. When I paste that version into the request body in the API Explorer, I get an error alert and it's clear from the color-coding in the body field that it is not correct. Even when I don't email the output to myself, I get the same error behavior, so I'm pretty sure that the act of emailing isn't messing things up.

Help!!


Solution

  • The invalid input is a result of a specific field not correclty formatted. In your case, I see that you are using custom schemas. There is a specific filed in the custom schema that is not properly formatted, that is startDate. Instead of providing the value of 2019-05-20T04:00:00.000Z, just use 2019-05-20.

    The reason for that is because the field is expecting a complete date only value instead of a complete date plus hours and minutes. As you can see on the ISO-8601 date format, the complete date format is YYYY-MM-DD.

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