使用Google Apps脚本将用户添加到Google网上论坛

Yaa*_*ler 3 google-api google-groups google-apps-script

我正在尝试使用Google Apps脚本将用户添加到Google网上论坛。

这是我尝试过的代码:

// Adds a user to a Google Group
function addUsertoGroup(userEmail) {
  var userEmail = 'Name@gmail.com'
  var groupId = "group-name@googlegroups.com";
  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);
Run Code Online (Sandbox Code Playgroud)

运行时,我收到一个错误:

对directory.groups.get的API调用失败,并出现以下错误:未找到域。

任何帮助,将不胜感激。

Sou*_*ria 5

Clarification:

  • You require a G Suite account to be able to use the AdminDirectory function (or to have the ability to add users to a group via Apps Script.
  • You can add gmail or any other non-same domain users based on the group setting you configure via https://groups.google.com

Solution:

I have a G Suite account via script.gs and I tested the code that you've shared - its perfect :) Except for the following that you need to enable, from a G Suite account.

Navigate to Resources > Advanced Google Services... and enable Admin Directory API

管理员目录API

That's it. I created the group, enabled all settings, made the group accessible to everyone and it worked like a charm.

Let me know if you require any further clarification or assist as well.

Edit notes:

So, this is also what I had to follow to ensure everyone (even folks outside the domain could be added as users), as documented on Set Groups for Business sharing options. When you go to Groups for Business and navigate through the settings, you'd get to enable the following option that's critical -

商业团体

Obviously, you're free to tweak all the other settings, as required.

  • @TheMaster - 是的,它与 G Suite API 也一样 - https://developers.google.com/admin-sdk/directory/v1/limits#api-limits-and-quotas `您不能创建超过 10 个用户使用目录 API 每秒域数。` (3认同)
  • 以编程方式添加的 Gmail 地址数量是否有限制?官方 [support](https://support.google.com/groups/answer/2465464) 手动显示为 10。从程序上来说这是真的吗?这也适用于 gsuite 吗? (2认同)
  • 我假设插入[用户](https://developers.google.com/admin-sdk/directory/v1/reference/users/insert)与将成员添加到[组](https://developers.google .com / admin-sdk / directory / v1 / reference / members / insert)。正如您在答案中所写的那样,您还可以将公共@gmail地址添加到该网上论坛。我问这个问题是因为我相信您需要为每位用户支付gsuite的费用。您可以添加100个Gmail地址吗?如果是这样,是否考虑对他们计费?PS:*您不必回答,我不是颁发赏金的人;)* (2认同)
  • 谢谢您的详尽回答!这非常好用。 (2认同)