Managing memberships for Google Groups

After a group exists, you can create memberships for it. This page explains how to perform some fundamental membership operations with the Cloud Identity Groups API. To learn how to create a Google Group, refer to Creating and listing Google Groups.

Before you begin

Set up authentication and install the client libraries. See Setting up the Cloud Identity Groups API to learn how.

Adding a membership to a Google Group

REST

To add a member to a Google Group, call groups.memberships.create() with the ID of the parent group and a memberships resource.

Python

The following code shows you how to associate a member with a group:


def create_google_group_membership(service, identity_source_id, group_id, member_key):
  param = "&groupKey.id=" + group_id + "&groupKey.namespace=identitysources/" + identity_source_id
  try:
    lookupGroupNameRequest = service.groups().lookup()
    lookupGroupNameRequest.uri += param
    # Given a group ID and namespace, retrieve the ID for parent group
    lookupGroupNameResponse = lookupGroupNameRequest.execute()
    groupName = lookupGroupNameResponse.get("name")
    # Create a membership object with a memberKey and a single role of type MEMBER
    membership = {
      "memberKey": member_key,
      "roles" : {
      "name" : "MEMBER"
      }
    }
    # Create a membership using the ID for the parent group and a membership object
    response = service.groups().memberships().create(parent=groupName, body=membership).execute()
    print response
  except Exception, e:
    print e

Listing memberships of a Google Group

REST

To list memberships in a Google Group, call groups.memberships.list() with the ID of the parent group and a memberships resource.

Python

The following code lists the memberships for a group:

def list_google_group_memberships(service, group_id):
  param = "&groupKey.id=" + group_id
  try:
    lookup_group_name_request = service.groups().lookup()
    lookup_group_name_request.uri += param
    lookup_group_name_response = lookup_group_name_request.execute()
    group_name = lookup_group_name_response.get("name")
    # List memberships
    response = service.groups().memberships().list(parent=group_name).execute()
    print response
  except Exception, e:
    print e