本示例介绍使用Alibaba Cloud SDK for Java调用GetVideoPlayAuth接口获取视频播放凭证。

前提条件

在调用GetVideoPlayAuth接口前,请确保。

  • 已经开通了视频点播服务。
  • 已完成域名绑定,详情参见添加域名
  • 已上传一个视频并获取到该视频的ID(videoId)。
  • 安装视频点播Java SDK。
    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>java.demo</groupId>
        <artifactId>test</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.4.3</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-vod -->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-vod</artifactId>
                <version>2.15.5</version>
            </dependency>
        </dependencies>
    </project>

示例代码

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.google.gson.Gson;

public class TestGetPlayInfo {

    public static void main(String[] args) {
        // 创建SubmitMediaInfoJob实例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>",           // 点播服务所在的地域ID,中国大陆地域请填cn-shanghai
                "<your-access-key-id>",       // 您的AccessKey ID
                "<your-access-key-secret>" ); // 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        // 视频ID,可以通过GetVideoList接口查询得到
        request.setVideoId("<your-video-id>");
        try {
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}