我正在尝试使用 Google Apps 脚本将用户添加到 Google 网上论坛。
这是我尝试过的代码:
// Adds a user to a Google Group
function addUsertoGroup(userEmail) {
var userEmail = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="014f606c6441666c60686d2f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>'
var groupId = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4c59445e5b06454a464e6b4c44444c474e4c59445e5b5805484446" rel="noreferrer noopener nofollow">[email protected]</a>";
var group = GroupsApp.getGroupByEmail(groupId);
// If email is already in group
try { var hasMember = group.hasUser(userEmail);}
catch(e){Logger.log(userEmail+" is already in the group"); return}
var newMember = {email: userEmail, role: "MEMBER"};
// This is the line which is throwing an error
AdminDirectory.Members.insert(newMember, groupId);
运行时,我收到错误:
API call to directory.groups.get failed with error: Domain not found.
如有任何帮助,我们将不胜感激。
最佳答案
澄清:
- 您需要 G Suite 帐户才能使用
AdminDirectory
功能(或能够通过 Apps 脚本将用户添加到群组。 - 您可以根据通过 https://groups.google.com 配置的群组设置添加
gmail
或任何其他非同一域用户
解决方案:
我通过 script.gs
拥有一个 G Suite 帐户,并且我测试了您共享的代码 - 非常完美:) 除了您需要从 G Suite 帐户启用的以下功能之外.
导航至资源 > 高级 Google 服务...并启用Admin Directory API
就是这样。我创建了该群组,启用了所有设置,让每个人都可以访问该群组,它的效果非常好。
如果您还需要任何进一步的说明或帮助,请告诉我。
编辑注释:
所以,这也是我必须遵循的,以确保每个人(甚至域外的人也可以添加为用户),如 Set Groups for Business sharing options 中所述。 。当您转到 Groups for Business并浏览设置,您可以启用以下关键选项 -
显然,您可以根据需要自由调整所有其他设置。
关于google-apps-script - 使用 Google Apps 脚本将用户添加到 Google 网上论坛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58136272/