创建和搜索 Google 群组

本页面介绍了如何使用 Cloud Identity Groups API 执行一些基本操作。

准备工作

在继续此页面的信息之前,请先执行以下任务:

创建 Google 群组

REST

如需创建 Google 群组,请使用新群组的实例调用 groups.create()。群组实例必须包含 groupKeyParent 和设置为 cloudidentity.googleapis.com/groups.discussion_forumlabel

您还需要设置 initialGroupConfig 参数,该参数定义群组的初始所有者。您可以为此参数使用以下值:

  • WITH_INITIAL_OWNER:使发送请求的人成为群组的所有者。大多数情况下,您都应该使用此值。
  • EMPTY:创建一个没有初始所有者的群组。只有 Google Workspace 超级用户或群组管理员才能使用此值。如需详细了解 Google Workspace 角色,请参阅预先创建的管理员角色

Python

以下示例展示了使用 Python 客户端库创建 Google 群组的辅助函数:

def create_google_group(service, customer_id, group_id, group_display_name, group_description):
  group_key = {"id": group_id}
  group = {
    "parent": "customers/" + customer_id,
    "description": group_description,
    "displayName": group_display_name,
    "groupKey": group_key,
    # Set the label to specify creation of a Google Group.
    "labels": {
      "cloudidentity.googleapis.com/groups.discussion_forum": ""
    }
  }

  try:
    request = service.groups().create(body=group)
    request.uri += "&initialGroupConfig=WITH_INITIAL_OWNER"
    response = request.execute()
    print(response)
  except Exception as e:
    print(e)

搜索 Google 群组

REST

如需搜索 Google 群组,请使用查询字符串调用 groups.search()。如需搜索所有群组,您只需提供标签 cloudidentity.googleapis.com/groups.discussion_forum

Python

以下示例展示了使用 Python 客户端库搜索 Google 群组的辅助函数:

from urllib.parse import urlencode

def search_google_groups(service, customer_id):
  search_query = urlencode({
          "query": "parent=='customerId/{}' && 'cloudidentity.googleapis.com/groups.discussion_forum' in labels".format(customer_id)
  })
  search_group_request = service.groups().search()
  param = "&" + search_query
  search_group_request.uri += param
  response = search_group_request.execute()

  return response

后续步骤