2

I'm trying to add a user to a Google Group using Google Apps Script.

Here's the code I've tried:

// 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);

When running, I receive an error:

API call to directory.groups.get failed with error: Domain not found.

Any help would be appreciated.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
  • Does this problem occur only for a specific userEmail or groupId? – ziganotschka Sep 27 '19 at 15:27
  • All emails – so yes, `groupId`. – Yaakov Bressler Sep 27 '19 at 15:36
  • Do you have gsuite and a domain? – TheMaster Sep 29 '19 at 16:41
  • 1
    Negative @TheMaster but I'm not opposed to purchasing one for this task. – Yaakov Bressler Sep 29 '19 at 17:08
  • 2
    I'm sure you need one. See [here](https://stackoverflow.com/a/21262115/) and [here](https://stackoverflow.com/a/37232037). But, I'm not sure whether you can add public `@gmail` addresses to the group. [Search](https://stackoverflow.com/search?q=%5Bgoogle-directory-api%5DAdd+member) and [search](https://stackoverflow.com/search?q=Add+member+%5Bgoogle-groups-api%5D). **Maybe others can help** – TheMaster Sep 29 '19 at 17:38
  • Answer is available here: https://stackoverflow.com/questions/56406314/does-fixing-a-domain-not-found-error-require-a-g-suite-account Cannot close because this question has bounty, but looks like a duplicate – Kos Sep 30 '19 at 02:35

2 Answers2

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

Admin Directory 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 -

Groups for Business

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

Sourabh Choraria
  • 2,255
  • 25
  • 64
  • 2
    Is there any limit on the number of gmail addresses that can be added programmatically? Official [support](https://support.google.com/groups/answer/2465464) says 10 manually. Is that true programmatically? Does that apply to gsuite too? – TheMaster Sep 30 '19 at 08:03
  • 3
    @TheMaster - Yes, its the same with G Suite API too - https://developers.google.com/admin-sdk/directory/v1/limits#api-limits-and-quotas `You cannot create more than 10 users per domain per second using the Directory API.` – Sourabh Choraria Sep 30 '19 at 08:14
  • 3
    I'm assuming inserting [users](https://developers.google.com/admin-sdk/directory/v1/reference/users/insert) is different from adding members to [group](https://developers.google.com/admin-sdk/directory/v1/reference/members/insert). As you've written in your answer, You can also add public @gmail addresses to the group. I'm asking this because I believe you need to pay per user in gsuite. Can you add a 100 gmail addresses? If so, Are they considered for billing? PS: *You don't have to answer, I'm not the one awarding the bounty;)* – TheMaster Sep 30 '19 at 08:28
  • 1
    @TheMaster - I understand and these are indeed serious questions :) To clarify, we can add `add gmail or any other non-same domain users` and there's no documented restriction specifically talking about the count of gmail users that you could add (I suspect there isn't one). As for G Suite users, you're right there too - that ought to be paid for, if it were the same domain; however, you are free to add any non-domain users (just like that of gmail). – Sourabh Choraria Sep 30 '19 at 08:30
-1

You are trying to use the AdminDirectory service, this service will work only for G Suite Admin within a G Suite domain. So you need to have your domain and it will work if you add user from your domain to a group of your domain.

i.e. user@domain.com added to group@domain.com

Based on your sample code you try to add a gmail.com user to a public Google Groups for that you can't use the admin directory API because you are not admin of the domain that manage the public groups.

And in this case, as you did, you can only use the GroupsApp service and this service only allow you to check if user is in the ggroups or what is the role.

=> With GroupsApp service it is impossible to add a user to a public google group.

Stéphane

St3ph
  • 2,232
  • 17
  • 17