google-apigoogle-groupsgoogle-chat

Need help adding a Google Group to a Google Chat Space


I'm admittedly a bit of a noob here, but trying to work through writing an API call to add a Google Group to a Google Chat space.

I've auth'd via OAuth 2.0 and have the right scopes. When I make a similar call to add just a single user (by email), that call works. But when I try to add a group, I get an error.

Thank you!

I believe I have the right scopes: https://www.googleapis.com/auth/chat.memberships https://www.googleapis.com/auth/chat.spaces

And here's my call

POST https://chat.googleapis.com/v1/spaces/[space_ID]/members

And I'm passing:
{    
      groupMember: {
        name: 'user/[email_address_of_the_group]'
      }
}

But I'm getting the following error:

Status code 400
{
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "status": "INVALID_ARGUMENT"
  }
}

Solution

  • Change:

    {
      groupMember: {
        name: 'user/[email_address_of_the_group]'
      }
    }
    

    To:

    {
      "groupMember": {
        "name": "groups/***************"
      }
    }
    

    Where "groups/***************" would come from the queried list of Google Groups from Method: groups.list.

    TL;DR

    The goal is to add a Google Group to a Google Chat space with an API call.

    You're encountering the Status code 400 because an email address is being used in 'user/[email_address_of_the_group]', which is invalid for the operation.

    As per Method: spaces.members.create:

    The resource name of the space for which to create the membership.

    This means that the unique identifier for the space needs to be used. To get it, query Method: groups.list first with the Customer ID from the Admin console. You may get it by going to Account > Account Settings > Profile or by going to this Admin console link.

    Afterwards, execute it as customers/C******** in parents, and it should return something like this:

    {
      "groups": [
        {
          "name": "groups/***************",
          "groupKey": {
            "id": "testgroup@domain.com"
          },
          "displayName": "Test Group"
        },
        {
          "name": "groups/***************",
          "groupKey": {
            "id": "test-1@domain.com"
          },
          "displayName": "test 1"
        },
        {
          "name": "groups/***************",
          "groupKey": {
            "id": "classroom_teachers@domain.com"
          },
          "displayName": "Classroom Teachers"
        },
        {
          "name": "groups/***************",
          "groupKey": {
            "id": "postmaster@domain.com"
          },
          "displayName": "postmaster"
        },
        {
          "name": "groups/***************",
          "groupKey": {
            "id": "abuse@domain.com"
          },
          "displayName": "abuse"
        }
      ]
    }
    

    The payload for the API call should look like this:

    {
      "groupMember": {
        "name": "groups/***************"
      }
    }
    

    REFERENCES