建立及搜尋 Google 群組
本頁面說明如何使用 Cloud Identity Groups API 執行一些基本作業。
事前準備
請先完成下列工作,再按照本頁資訊操作:
建立 Google 群組
REST
如要建立 Google 群組,請使用新群組的執行個體呼叫 groups.create()。群組執行個體必須包含 groupKey、Parent 和 label,且這些值都設為 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
後續步驟
群組建立完成後,即可建立成員資格。如要建立 Google 群組的成員資格,請參閱「管理 Google 群組成員資格」。