使用 App Script 和 Admin SDK 设置 G Suite 用户属性

Setting G Suite user properties with App Script and the Admin SDK

我正在尝试通过 App Maker 中的 App Script 设置用户的 OU。 (user 是一个带有电子邮件地址的变量)

function getUser(user) {
  var x = AdminDirectory.Users.update(
    {
      orgUnitPath: "/",
      userKey: user,
    });
  console.log("function ran");
}

此代码错误:

Exception: Invalid number of arguments provided. Expected 2-3 only at getUser (ServerScripts:107)
Invalid number of arguments provided. Expected 2-3 only
at getUser (ServerScripts:107)
at getUser (ClientHandoff:21:21)
at TestMoveOU.Panel1.Button1.onClick:1:1

我在这里做错了什么?查看 the docs,您只需提供要更改的属性。

Apps Script documentation 表示如下:

For detailed information on this service, see the reference documentation for the Admin SDK Directory API. Like all advanced services in Apps Script, the Admin SDK Directory service uses the same objects, methods, and parameters as the public API.

因此,我们需要参考 documentation 来了解如何实现这一点。

该方法至少需要两个参数:即第一个参数是用户对象资源,第二个参数是用户的邮箱地址:AdminDirectory.Users.update(resource, userKey)。所以你需要这样做:

function getUser(user) {
    var userResource = {
        orgUnitPath: "/"
    };
    var updated = AdminDirectory.Users.update(userResource, user);
    console.log(updated.primaryEmail);
}

既然已经在 userResource 对象中指定了,为什么还需要在方法中指定用户电子邮件?那么,userResource 对象中的电子邮件地址将是新值,以防您想更改电子邮件地址。

P.S。也许您可能想将函数的名称更改为更匹配的名称; updateUser() 也许吧?希望对您有所帮助!