建立及搜尋 Google 群組

本頁面說明如何使用 Cloud Identity Groups API 執行一些基本作業。

事前準備

請先執行下列工作,再繼續瞭解本頁資訊:

建立 Google 群組

REST

如要建立 Google 群組,請使用新群組的例項呼叫 groups.create()。群組例項必須包含 groupKeyParentlabel,並設為 cloudidentity.googleapis.com/groups.discussion_forum

您也需要設定 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

後續步驟