UserManager 服务不支持组织单位,但您可以手动构建请求。
以下 Apps 脚本代码将用户添加到 OU。参数是customerId、要添加到组织单位的用户的电子邮件地址和组织单位路径:
function addUserToOU(customerId, email, ou) {
var oauthConfig = UrlFetchApp.addOAuthService("google");
var scope = "https://apps-apis.google.com/a/feeds/policies/";
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
var body = "<?xml version=\"1.0\" encoding=\"utf-8\"?><atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:apps=\"http://schemas.google.com/apps/2006\"><apps:property name=\"orgUnitPath\" value=\"" + ou + "\" /></atom:entry>";
var requestData = {
"method": "put",
"contentType": "application/atom+xml",
"oAuthServiceName": "google",
"oAuthUseToken": "always",
"payload": body
};
var url = "https://apps-apis.google.com/a/feeds/orguser/2.0/" + customerId + "/" + email;
var result = UrlFetchApp.fetch(url, requestData);
Logger.log(result.getContentText());
}