Query group IDs

更新时间:
复制 MD 格式

Use the Content Moderation Python SDK to retrieve the IDs of all person groups in your account.

Prerequisites

Before you begin, ensure that you have:

  • Python dependencies installed for the required Python version. See Installation for version requirements. Using an incorrect Python version causes subsequent API calls to fail.

  • The Extension.Uploader utility class downloaded and imported into your project.

Usage notes

  • Use the Content Moderation API endpoints when calling this SDK. See Endpoints for details.

  • Reuse the AcsClient instance across calls to improve performance and avoid repeated connections.

  • Store your AccessKey ID and AccessKey secret as environment variables. Avoid hardcoding credentials in your code.

  • For the full list of request parameters, see GetGroups.

Query all person group IDs

The following example calls GetGroupsRequest to retrieve all person group IDs and prints them to the console.

# coding=utf-8
import os
import json
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import GetGroupsRequest

# Initialize the client using AccessKey credentials from environment variables.
# Reuse this client instance to improve performance and avoid repeated connections.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET')

clt = client.AcsClient(access_key_id, access_key_secret, 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')

# Build and send the request.
request = GetGroupsRequest.GetGroupsRequest()
request.set_accept_format('JSON')

response = clt.do_action(request)
result = json.loads(response)

# Parse the response.
if result['code'] == 200:
    result_data = result['data']
    if result_data['code'] == 200:
        # Status code 200 indicates that group IDs were retrieved successfully.
        group_ids = result_data['groupIds']
        print(group_ids)

Response structure

The response is a nested JSON object:

FieldTypeDescription
codeintegerTop-level status code. 200 indicates a successful request.
data.codeintegerInner status code. 200 indicates that group IDs were retrieved.
data.groupIdsarrayList of person group IDs in your account.