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

开始之前

在调用GetVideoPlayAuth接口前,确保:
  • 已经开通了视频点播服务。
  • 已完成域名绑定,详情参见域名管理
  • 已上传一个视频并获取到该视频的视频ID(videoId)。
  • 安装视频点播Java SDK,详情参见安装SDK

示例代码

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;


public class GetPlayAuthDemo {

    // 设置AccessKey ID和AccessKey secret
    private static String access_key_id = "";
    private static String access_key_secret = "";

    public static void main(String[] args) {
        // 点播服务所在的地域,中国大陆地域请填cn-shanghai
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", access_key_id, access_key_secret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        // 传入视频ID 
        GetVideoPlayAuthResponse response = getVideoPlayAuth(client, "995bde680981499181f5cd7472dd9c61");
        System.out.println(response.getPlayAuth());
    }

    private static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client, String videoId) {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        GetVideoPlayAuthResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ServerException e) {
            throw new RuntimeException("GetVideoPlayAuthRequest Server failed");
        } catch (ClientException e) {
            throw new RuntimeException("GetVideoPlayAuthRequest Client failed");
        }
        return response;
    }
}