# 获取频道API的访问令牌

### 接口描述

```
1、接口用于获取频道级别的API访问令牌（channel access token），以下简称token
2、token可以用于访问指定一个频道号的API，并且访问路径需符合以下模式：/live/v3/channel/**，未来会逐步扩展其功能
3、token可以用于接入直播平台一些特定的SDK
4、接口支持https协议
```

### 接口URL

```
https://api.polyv.net/live/v3/common/token/get-channel-token 
```

[在线API调用](https://git.polyv.net/help-center/document-center/-/blob/master/req.html?api=http:/api.polyv.net/live/v3/common/token/get-channel-token/README.md)

### 请求方式

```
POST
```

### 接口约束

1、接口同时支持HTTP 、HTTPS ，建议使用HTTPS 确保接口安全，接口调用有频率限制，[详细请查看](https://git.polyv.net/help-center/document-center/-/blob/master/live/api/limit/README.md)

2、单个频道在1个小时之内调用接口的次数不得超过500次，限制机制在未来可能会有所调整，建议开发者在服务端统一缓存token，不要频繁调用接口

3、token存在有效期，开发者需要自行维护更新，可以根据接口返回的失效时间主动刷新token，亦或者在token失效后重新获取，注意不要多处重复调用接口

4、至少留出512字节给channelToken

### 请求参数描述

| 参数名           | 必选    | 类型      | 说明                                                                                                                                                                                                        |
| ------------- | ----- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| appId         | true  | String  | 账号appId【详见[获取密钥](https://git.polyv.net/help-center/document-center/-/blob/master/live/api/getSecretKey/README.md)】                                                                                        |
| timestamp     | true  | Long    | 当前13位毫秒级时间戳，3分钟内有效                                                                                                                                                                                        |
| sign          | true  | String  | 签名，为32位大写的MD5值,`生成签名的appSecret密钥作为通信数据安全的关键信息，严禁保存在客户端直接使用，所有API都必须通过客户自己服务器中转调用POLYV服务器获取响应数据`【详见[签名生成规则](https://git.polyv.net/help-center/document-center/-/blob/master/live/api/buildSign/README.md)】 |
| channelId     | true  | String  | 频道号                                                                                                                                                                                                       |
| disposable    | false | Boolean | 是否一次性有效，true/false，默认为false                                                                                                                                                                               |
| expireSeconds | false | Integer | token有效时长，单位为秒，取值范围：0 < expireSeconds <= 3600，默认为1800秒                                                                                                                                                    |

### 示例

```request
Request URL: https://api.polyv.net/live/v3/common/token/get-channel-token
Request Method: POST
Content-Type: application/x-www-form-urlencoded
Form Data: appId=<yourAppId>&timestamp=1623308604852&sign=099846EF3DD92402DF9C60804556CE37&channelId=1762528
```

### 响应参数描述

| 参数名     | 类型      | 说明                                                                                                                                                                 |
| ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| code    | Integer | 响应状态码，200为成功返回，非200为失败【详见[全局错误说明](https://git.polyv.net/help-center/document-center/-/blob/master/live/api/errorInfo/README.md)】                                   |
| status  | String  | 响应状态文本信息                                                                                                                                                           |
| message | String  | 响应描述信息，当code为400或者500的时候，辅助描述错误原因                                                                                                                                  |
| data    | Object  | 请求成功时为token信息【详见[Data参数描述](https://git.polyv.net/help-center/document-center/-/blob/master/live/api/channel/get_channel_api_access_token.md?id=polyv1)】，请求失败时为null |

[**Data参数描述**](#/live/api/channel/get_channel_api_access_token.md?id=polyv1)

| 参数名          | 类型     | 说明                                                                |
| ------------ | ------ | ----------------------------------------------------------------- |
| expireTime   | Long   | 失效时间，13位毫秒级时间戳，在该时间之前的token有效，否则所调用接口将返回code=15, 代表无效token，需要重新获取 |
| channelToken | String | 频道接口访问令牌，为了避免权限过大而导致误操作，该token仅限访问该频道数据，否则所调用接口将返回无效token的错误      |

使用token调用频道接口时，传了无效token的返回结果示例

```json
{
    "code": 15,
    "status": "error",
    "message": "invalid token.",
    "data": null
}
```

### Java请求示例

快速接入基础代码请下载相关依赖源码， [点击下载源代码](https://git.polyv.net/help-center/document-center/-/blob/master/third_res/util.zip) ,下载后加入到自己的源码工程中即可。测试用例中的**HttpUtil.java 和 LiveSignUtil.java** 都包含在下载文件中。

> 强烈建议您使用[直播Java SDK](https://git.polyv.net/help-center/document-center/-/blob/master/live/java/README.md)完成API的功能对接，直播Java SDK 对API调用逻辑、异常处理、数据签名、HTTP请求线程池进行了统一封装和优化。

```java
private static final Logger log = LoggerFactory.getLogger(getClass());
/**
 * 获取频道API的访问令牌
 * @throws Exception
 */
@Test
public void testGetChannelApiToken() throws Exception {
    //公共参数,填写自己的实际参数
    String appId = super.appId;
    String appSecret = super.appSecret;
    String timestamp = String.valueOf(System.currentTimeMillis());
    //业务参数
    String url = "https://api.polyv.net/live/v3/common/token/get-channel-token";
    String channelId = "1762528";

    Map<String,String> requestMap = new HashMap<>();
    requestMap.put("appId", appId);
    requestMap.put("timestamp",timestamp);
    requestMap.put("channelId",channelId);
    requestMap.put("sign",LiveSignUtil.getSign(requestMap, appSecret));
    String response = HttpUtil.postFormBody(url, requestMap);
    log.info("获取频道API的访问令牌，返回值：{}",response);
}
```

### 响应示例

系统全局错误说明详见[全局错误说明](https://git.polyv.net/help-center/document-center/-/blob/master/live/api/errorInfo/README.md)

成功示例

```json
{
    "code": 200,
    "status": "success",
    "message": "",
    "data": {
        "channelToken": "65027cb0765c4d07afad13ba60467af2",
        "expireTime": 1623238164635
    }
}
```

异常示例

```json
{
    "code": 400,
    "status": "error",
    "message": "invalid signature.",
    "data": ""
}
```

api 调用频率限制

```json
{
    "code": 400,
    "status": "error",
    "message": "qps exceeds number of calls, limit: %s",
    "data": ""
}
```

expireSeconds值受限制，必须 0 < expireSeconds <= 3600

```json
{
    "code": 400,
    "status": "error",
    "message": "expireSeconds limited.",
    "data": ""
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://polyv.gitbook.io/document/docs/live/api/channel/auth/get_channel_api_access_token.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
