EMPTY:创建没有初始所有者的群组。只有 Google Workspace 超级用户或群组管理员才能使用此值。如需详细了解 Google Workspace 角色,请参阅预先创建的管理员角色。
Python
以下示例展示了使用 Python 客户端库创建 Google 群组的辅助函数:
defcreate_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)exceptExceptionase:print(e)
搜索 Google 群组
REST
如需搜索 Google 群组,请使用查询字符串调用 groups.search()。如需搜索所有群组,您只需提供标签 cloudidentity.googleapis.com/groups.discussion_forum。
Python
以下示例展示了使用 Python 客户端库搜索 Google 群组的辅助函数:
fromurllib.parseimporturlencodedefsearch_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_querysearch_group_request.uri+=paramresponse=search_group_request.execute()returnresponse
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eThis page details the process of creating and searching for Google Groups using the Cloud Identity Groups API, highlighting the use of REST and Python client library methods.\u003c/p\u003e\n"],["\u003cp\u003eTo create a Google Group, you must use the \u003ccode\u003egroups.create()\u003c/code\u003e method, providing a \u003ccode\u003egroupKey\u003c/code\u003e, \u003ccode\u003eParent\u003c/code\u003e, and the label \u003ccode\u003ecloudidentity.googleapis.com/groups.discussion_forum\u003c/code\u003e, as well as defining an initial owner via the \u003ccode\u003einitialGroupConfig\u003c/code\u003e parameter.\u003c/p\u003e\n"],["\u003cp\u003eSearching for Google Groups involves calling the \u003ccode\u003egroups.search()\u003c/code\u003e method with a query string that includes the \u003ccode\u003ecloudidentity.googleapis.com/groups.discussion_forum\u003c/code\u003e label, and optionally a parent customer ID to filter results.\u003c/p\u003e\n"],["\u003cp\u003eBefore utilizing the Cloud Identity APIs, including the Groups API, it's crucial to complete the necessary setup steps as outlined in the "Setting up Cloud Identity" guide and the "Set up the Groups API" guide.\u003c/p\u003e\n"],["\u003cp\u003eThe customer ID format for these methods requires prepending a 'C' to the ID that is found in the Google Admin console.\u003c/p\u003e\n"]]],[],null,["# Creating and searching for Google Groups\n========================================\n\nThis page explains how to perform some fundamental operations with the\nCloud Identity Groups API.\n\nBefore you begin\n----------------\n\n| **Note:** Before you use any of the Cloud Identity APIs, you must set up Cloud Identity. See [Setting up Cloud Identity](/identity/docs/set-up-cloud-identity-admin) for instructions.\n\nPerform the following tasks before proceeding with the information on this page:\n\n- Read the [Groups API overview](/identity/docs/groups).\n\n- [Set up the Groups API](/identity/docs/how-to/setup).\n\n| **Note:** When a method requires the `customer_id`, you must prepend a \"C\" to the ID that you can view in the Google Admin console (for example, 'C046psxkn').\n\nCreating a Google Group\n-----------------------\n\n### REST\n\nTo create a Google Group, call\n[`groups.create()`](/identity/docs/reference/rest/v1/groups/create) with\nan instance of the new group. The group instance must include a `groupKey`,\n`Parent`, and `label` set to `cloudidentity.googleapis.com/groups.discussion_forum`.\n\nYou also need to set the `initialGroupConfig` parameter, which defines the\ninitial owner of the group. You can use the following values for this\nparameter:\n\n- `WITH_INITIAL_OWNER`: Makes the person sending the request the owner of the group. You should use this value in most cases.\n- `EMPTY`: Creates a group with no initial owners. You can only use this value if you're a Google Workspace Super Admin or Groups Admin. For more information about Google Workspace roles, see [Pre-built administrator\n roles](https://support.google.com/a/answer/2405986).\n\n### Python\n\nThe following example shows a helper function to create a Google Group using\nthe Python client library: \n\n def create_google_group(service, customer_id, group_id, group_display_name, group_description):\n group_key = {\"id\": group_id}\n group = {\n \"parent\": \"customers/\" + customer_id,\n \"description\": group_description,\n \"displayName\": group_display_name,\n \"groupKey\": group_key,\n # Set the label to specify creation of a Google Group.\n \"labels\": {\n \"cloudidentity.googleapis.com/groups.discussion_forum\": \"\"\n }\n }\n\n try:\n request = service.groups().create(body=group)\n request.uri += \"&initialGroupConfig=WITH_INITIAL_OWNER\"\n response = request.execute()\n print(response)\n except Exception as e:\n print(e)\n\nSearching for a Google Group\n----------------------------\n\n### REST\n\nTo search for a Google Group, call\n[`groups.search()`](/identity/docs/reference/rest/v1/groups/search) with\na query string. To search for all groups, you only need to provide the label\n`cloudidentity.googleapis.com/groups.discussion_forum`.\n\n### Python\n\nThe following example shows a helper function used to search for a Google\nGroup using the Python client library: \n\n from urllib.parse import urlencode\n\n def search_google_groups(service, customer_id):\n search_query = urlencode({\n \"query\": \"parent=='customerId/{}' && 'cloudidentity.googleapis.com/groups.discussion_forum' in labels\".format(customer_id)\n })\n search_group_request = service.groups().search()\n param = \"&\" + search_query\n search_group_request.uri += param\n response = search_group_request.execute()\n\n return response\n\nWhat's next\n-----------\n\n- After a group exists, you can create memberships for it. To create\n memberships for a Google Group, refer to\n [Managing memberships for Google Groups](/identity/docs/how-to/memberships-google-groups).\n\n- You can [update a Google Group to a security group](/identity/docs/how-to/update-group-to-security-group)."]]