# 管理频道和用户 (/zh-CN/realtime-media/rtc/build/security-and-auth/channel-management)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

为方便开发者从服务端管理实时音视频频道，声网提供了集成简单、安全可靠的服务端 RESTful API。

具体的功能和适用场景如下：

| 主要功能 | 功能描述 | 典型适用场景 |
| --- | --- | --- |
| 管理用户权限 | 通过调用 RESTful API 管理用户的权限，包括将指定用户移出频道、禁止指定用户发送音频流、禁止指定用户发送视频流。 | [channel-management-restful-html-0-1-2 details](#table-slot-channel-management-restful-html-0-1-2) |
| 查询在线频道信息 | 通过调用 RESTful API 查询在线频道列表、频道内的用户列表、指定用户的状态。 | [channel-management-restful-html-0-2-2 details](#table-slot-channel-management-restful-html-0-2-2) |

<a id="table-slot-channel-management-restful-html-0-1-2"></a>

**channel-management-restful-html-0-1-2 details**

需要在业务服务端管理用户权限的场景，例如：

- 直播中出现未经认证的用户。
-  直播中出现炸房捣乱的用户。
-  向客户端下发的信令消息被劫持。
- 用户异常掉线，导致用户列表无法及时更新。

<a id="table-slot-channel-management-restful-html-0-2-2"></a>

**channel-management-restful-html-0-2-2 details**

- 在频道并发量不大的场景下，直接查询项目下的频道列表以同步频道状态。
- 在同步实时性要求不高的场景下，查询并同步频道内的用户列表和状态。

## 前提条件 [#前提条件]

- 有效的声网开发者账号和声网控制台项目。详见[开通 RTC 服务](/zh-CN/realtime-media/rtc/build/setup-and-access/enable-service)。
- 已经[实现 HTTP 基本认证](/zh-CN/realtime-media/rtc/build/setup-and-access/http-basic-auth)。

## 实现方法 [#实现方法]

所有频道管理 RESTful API 都通过 `http://api.sd-rtn.com` 访问。以[查询项目的频道列表](/zh-CN/api-reference/api-ref/rtc/query-channel-list)为例，在实现 HTTP 基本认证后，添加以下高亮区域的代码即可发送一个简单的请求，获取频道总数和每个频道内的用户人数：

```java
public class Base64Encoding {

    public static void main(String[] args) throws IOException, InterruptedException {

        // 客户 ID
        // 需要设置环境变量 AGORA_CUSTOMER_KEY
        final String customerKey = System.getenv("AGORA_CUSTOMER_KEY");
        // 客户密钥
        // 需要设置环境变量 AGORA_CUSTOMER_SECRET
        final String customerSecret = System.getenv("AGORA_CUSTOMER_SECRET");

        // App ID
        // 需要设置环境变量 AGORA_APP_ID
        final String appid = System.getenv("AGORA_APP_ID");

        // 拼接客户 ID 和客户密钥并使用 base64 编码
        String plainCredentials = customerKey + ":" + customerSecret;
        String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
        // 创建 authorization header
        String authorizationHeader = "Basic " + base64Credentials;

        // highlight-start
        HttpClient client = HttpClient.newHttpClient();

        // 创建 HTTP 请求对象
        HttpRequest request = HttpRequest.newBuilder()
               .uri(URI.create("http://api.sd-rtn.com/dev/v1/channel/" + appid))
               .GET()
               .header("Authorization", authorizationHeader)
               .header("Content-Type", "application/json")
               .build();
        // 发送 HTTP 请求
        HttpResponse<String> response = client.send(request,
                HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
        // 添加后续对响应内容的处理逻辑
        // highlight-end
   }
}
```

你可以参考 API 文档查看每个频道管理 RESTful API 的详情，其中包含了多种语言的示例代码：

- [封禁用户权限](/zh-CN/api-reference/api-ref/rtc/create-ban-rule)
- [查询在线频道消息](/zh-CN/api-reference/api-ref/rtc/query-user-status)

## 更多功能 [#更多功能]

你可以根据业务需求，将频道管理 RESTful API 和[消息通知服务](/zh-CN/realtime-media/rtc/build/optimize-and-operate/receive_webhook)结合使用，实现稳定可靠、实时有效的频道管理和状态同步。

## 相关文档 [#相关文档]

- [封禁用户权限](/zh-CN/realtime-media/rtc/build/security-and-auth/user-privilege)：了解封禁用户权限 API 的使用原则、适用场景、不适用场景以及接口调用异常处理的最佳实践。
- [保障 REST 服务高可用](/zh-CN/realtime-media/rtc/build/optimize-and-operate/rest-availability)：如何避免因区域网络故障造成的服务不可用。
