> For the complete documentation index, see [llms.txt](https://polyv.gitbook.io/document/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://polyv.gitbook.io/document/docs/vod/api/vert/add_ad.md).

# 创建广告

### 接口描述

```
1、通过分类id为分类下的所有视频创建广告
2、接口URL中的{userid}为点播账号userid，具体参考菜单【使用须知】->【获取密钥】
3、接口支持https协议
```

### 接口URL

```
http://api.polyv.net/v2/advertising/{userid}/create
```

### 请求方式

```
POST
```

### 接口约束

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

### 请求参数描述

| 参数名             | 必选    | 类型      | 说明                                                                                                                                                                                                        |
| --------------- | ----- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| userid          | true  | String  | 保利威点播账户id，可以参考【[获取密钥](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/getSecretKey/README.md)】获取，获取路径：官网->登录->点播（API接口）                                                           |
| ptime           | true  | Long    | 当前时间的毫秒级时间戳，3分钟内有效                                                                                                                                                                                        |
| sign            | true  | String  | 签名，为40位大写的SHA1值,`生成签名的secretkey密钥作为通信数据安全的关键信息，严禁保存在客户端直接使用，所有API都必须通过客户自己服务器中转调用POLYV服务器获取响应数据`【详见[签名生成规则](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/buildSign/README.md)】 |
| startDate       | true  | String  | 广告开始日期，格式为：yyyy-MM-dd，例如：2021-04-07                                                                                                                                                                       |
| endDate         | true  | String  | 广告结束日期，格式为：yyyy-MM-dd，例如：2021-04-10                                                                                                                                                                       |
| title           | true  | String  | 广告标题                                                                                                                                                                                                      |
| file            | true  | File    | <p>广告素材<br>片头、片尾广告支持：JPEG，GIF，PNG，FLV，MP4<br>暂停广告支持：SWF，PNG，JPEG，GIF<br>弹窗广告支持：PNG，JPEG，GIF</p>                                                                                                           |
| size            | false | Integer | 广告时长，除请求参数location为暂停广告外，其他广告类型都为必填参数，单位秒                                                                                                                                                                 |
| cataids         | false | String  | 分类id，多个分类id，以英文逗号分隔，默认值为1，1是默认分类                                                                                                                                                                          |
| location        | false | Integer | <p>广告类型，默认为片头<br>1：片头<br>2：暂停<br>3：片尾<br>4：弹窗</p>                                                                                                                                                         |
| popLocation     | false | Integer | <p>广告弹窗位置，当请求参数location为弹窗时必填<br>1：右下角<br>2：右上角<br>3：左下角<br>4：左上角</p>                                                                                                                                     |
| popUpTime       | false | Integer | 弹窗出现的时间，单位秒，当请求参数location为弹窗时必填                                                                                                                                                                           |
| status          | false | Integer | <p>广告状态，默认为已上线<br>10：已上线<br>1：待上线<br>0：已下线</p>                                                                                                                                                            |
| upTime          | false | String  | 广告开始时间，格式为：HH:mm:ss，默认为：00:00:00                                                                                                                                                                          |
| offTime         | false | String  | 广告结束时间，格式为：HH:mm:ss，默认为：23:59:59                                                                                                                                                                          |
| note            | false | String  | 广告描述                                                                                                                                                                                                      |
| skipAd          | false | String  | <p>是否开启跳过广告，仅当请求参数location为片头广告有效，默认值为N<br>Y：开启<br>N：关闭</p>                                                                                                                                               |
| skipOffset      | false | String  | 多少秒后允许跳过，当请求参数skipAd为Y时，该字段为必填参数                                                                                                                                                                          |
| skipButtonLabel | false | String  | 跳过按钮标签描述，默认为跳过                                                                                                                                                                                            |

### 示例

```requestUrl
http://api.polyv.net/v2/advertising/1b448be323/create
```

表单参数：

```requestUrl
note=测试广告的描述内容&endDate=2021-4-8&popLocation=1&sign=AD41245052F42EF9D1FE41FA413948AFB60EAFCF&title=接口测试接口广告&skipAd=Y&upTime=09:45:00&cataids=1615286323771&popUpTime=2&skipButtonLabel=跳过&size=4&location=1&offTime=09:50:00&skipOffset=3&ptime=1617759804786&startDate=2021-4-7&status=10
```

### 响应参数描述

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

### 返回错误代码列表

| 响应代码 | message                                | 说明                                     |
| ---- | -------------------------------------- | -------------------------------------- |
| 400  | sign can not be empty.                 | 加密串为空                                  |
| 400  | ptime is too old.                      | 时间戳过期                                  |
| 400  | ptime is illegal.                      | 时间戳参数格式不对或者超过当前时间3分钟                   |
| 400  | Could not find user by userid.         | userid不存在                              |
| 400  | the sign is not right.                 | 加密串不正确                                 |
| 400  | file is empty.                         | 没上传文件或为空文件                             |
| 400  | illegal file type.                     | 非法的文件类型                                |
| 400  | upload file failed.                    | 上传文件失败                                 |
| 400  | startDate or endDate error.            | 日期格式不正确或开始日期大于结束日期                     |
| 400  | upTime or offTime error.               | 时间格式不正确或开始时间大于结束时间                     |
| 400  | param status can not be empty.         | 广告状态不能为空                               |
| 400  | title can not be empty.                | 广告标题不能为空                               |
| 400  | cataids length is not right.           | 分类数量不正确                                |
| 400  | exist the same cataid.                 | 存在相同的cataid                            |
| 400  | cataid type error.                     | cataid类型错误                             |
| 400  | cata not exist.                        | 分类不存在                                  |
| 400  | cata conflict.                         | 不能同时存在父分类和子分类                          |
| 400  | param location error.                  | 广告类型错误                                 |
| 400  | param addrurl error.                   | 链接地址错误                                 |
| 400  | pop location error.                    | 弹窗位置错误                                 |
| 400  | pop up time error.                     | 弹窗出现时间错误                               |
| 400  | illegal skipOffset.                    | 开启了跳过广告，但skipOffset参数不正确(不得小于0或大于广告时长) |
| 400  | location type not supported to skipAd. | 广告类型不支持开启跳过广告                          |
| 400  | save fail.                             | 服务器异常导致失败                              |

### Java请求示例

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

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

```java
private static final Logger log = LoggerFactory.getLogger(VodVideoAdvertising.class);
/**
 *  创建广告
 */
@Test
public void testAddAdvertising() throws Exception, NoSuchAlgorithmException {
    //公共参数,填写自己的实际参数
    String secretKey = super.secretKey;
    String userid = super.userId;
    String ptime = String.valueOf(System.currentTimeMillis());
    //业务参数
    String url = String.format("http://api.polyv.net/v2/advertising/%s/create", userid);
    String startDate = "2021-4-7";
    String endDate = "2021-4-8";
    String title = "接口测试接口广告";
    String advertisingPath =  getClass().getResource("/file/").getPath() + "advertisement.jpeg";
    File file = new File(advertisingPath);
    String size = "4";
    String cataids = "1615286323771";
    String location = "1";
    String popLocation = "1";
    String popUpTime = "2";
    String status = "10";
    String upTime = "09:45:00";
    String offTime = "09:50:00";
    String note = "测试广告的描述内容";
    String skipAd = "Y";
    String skipOffset = "3";
    String skipButtonLabel = "跳过";

    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("ptime", ptime);
    requestMap.put("startDate", startDate);
    requestMap.put("endDate", endDate);
    requestMap.put("title", title);
    Map<String,File> fileMap = new HashMap<>();
    fileMap.put("file",file);
    requestMap.put("size", size);
    requestMap.put("cataids", cataids);
    requestMap.put("location", location);
    requestMap.put("popLocation", popLocation);
    requestMap.put("popUpTime", popUpTime);
    requestMap.put("status", status);
    requestMap.put("upTime", upTime);
    requestMap.put("offTime", offTime);
    requestMap.put("note", note);
    requestMap.put("skipAd", skipAd);
    requestMap.put("skipOffset", skipOffset);
    requestMap.put("skipButtonLabel", skipButtonLabel);
    requestMap.put("sign", VodSignUtil.getSign(requestMap, secretKey));

    String response = HttpUtil.postFile(url, requestMap,fileMap,Constant.UTF8);
    log.debug("测试创建广告,{}", response);
    //do somethings

}
```

### 响应示例

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

成功示例

```json
{
	"code": 200,
	"status": "success",
	"message": "success",
	"data": "f48271dff47e4ab2a414"
}
```

异常示例

```json
{
  "code": 400,
  "status": "error",
  "message": "ptime is too old.",
  "data": ""
}
```
