Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update user with Admin SDK

I am trying to update some user data via the admin SDK. I thought this would work

function directoryUpdate(userId, userDept, userLocation, userPhone, userTitle) {
  var update = {
    organizations: 
      {
        name: "Next Step Living",
        title: userTitle,
        primary: true,
        type: "work",
        department: userDept,
        location: userLocation
      },
    phones: 
      {
        value: userPhone,
        type: "work",
        primary: true,
      }
  };
  update = AdminDirectory.Users.update(update, userId);

  Logger.log('User %s updated with result %s.', userId, update)
  return true;
}

but it is not updating the organization or phone data on the record. It also does not throw any kind of error.

three questions, what is the proper syntax to do this update, I assume this works like the API update and behaves like an upsert, is that true, and what is the best practice for capturing any errors during the update. I would like to return a false when the update fails and capture that info. Thanks in advance for your help.

like image 522
Jim F Avatar asked Oct 28 '25 14:10

Jim F


1 Answers

Thanks for your question! This "inspired" me to work out how the update API worked, as I had got as far as retrieving a User object, updating the properties but had not worked out how to persist the data back to Google.

So, here's my prototype code, which appears to work (the objective being to reset the user's password based on entries in a spreadsheet).

It doesn't seem the most elegant code to me, being that there are two round-trips to the Admin API, and we have to post the email address twice, but I guess that is a side-effect of the JSON API.

var emailAddress = userListSheet.getRange(row, 1).getValue();
var password = userListSheet.getRange(row, 2).getValue();
Logger.log('Email: %s, Password: %s', emailAddress, password);

// Reset user's password
var user = AdminDirectory.Users.get(emailAddress);
user.password = password;
if (changePasswordAtNextLogin == 'Yes') {
  user.changePasswordAtNextLogin = true;
}
AdminDirectory.Users.update(user, emailAddress);
like image 82
user3442947 Avatar answered Oct 31 '25 12:10

user3442947



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!