事件通知
所有文档
menu

视频创作分发平台 VideoWorks

事件通知

在VideoWorks中使用工作流处理媒资时,可以按照工作流中的通知设置,向待通知地址发送预定格式的通知消息,表示媒资处理已完成或失败。

在工作流中,除了开始节点,其他节点都可以设置通知。在此工作流运行中,设置了通知的节点运行结束后,会向通知指定地址发送一个通知消息,客户收到消息后,可以按自己的业务逻辑进行相应处理。

在工作流中设置通知前,需要先在控制台通知管理中添加可用的通知。通知地址支持http和https协议,https协议的通知地址需要客户预先准备好证书配置,在进行通知设置前,建议先测试下通知的endpoint是否能正常接受请求。通知请求的body内容如下:

{
  "id" : "ntf-xxxx",
  "endpoint" : "<your notification endpoint>",
  "content" : "<notification content>",
  "expire" : 1565235600000
}

body的每个参数说明如下:

参数 说明
id 通知id
endpoint 用户设置的通知地址
content 通知内容
+ mediaId 处理的媒资id
+ workflowId 处理的工作流id
+ workflowName 处理的工作流名称
+ instnaceId 运行的实例Id
+ instanceStatus 运行的实例状态,SUCCESS/FAILED
+ taskResult 发送通知的节点运行信息
expire 通知发送过期时间

如果设置了通知验证,通知的请求header中会附带验证信息,用户可以通过验证header的合法性来判断通知请求是否是第三方恶意请求。

例如,用户在VideoWorks的Console中创建了一个通知,通知设置是这样的:

{
    "notificationId": "not-xxxxxxx",
    "name": "noti",
    "endpoint": "http://qwe.com/vw/callback",
    "status": "ENABLE",
    "token": "qweASD123",
    "authType": "SIGN",
}

用户的userId为e95e33a028bd49dbb3e08f068dc975d5(可以在Console-用户中心获取)。

用户将此通知设置到一个工作流的发布节点中,在这个工作流处理完成后,VideoWorks将会发送通知给用户。在此实例中,通知的内容如下:

POST /vw/callback HTTP/1.1
notification-auth-expire: 1572923085545
notification-auth-user: e95e33a028bd49dbb3e08f068dc975d5
notification-auth-token: 67d987295025dccf2ea669b68e0eb5427009e0cb26b8663b19378d3ac77fec64
content-type: application/json;charset=UTF-8
content-length: 155
host: qwe.com
accept: */*
user-agent: AHC/2.0

{"mediaId":"mda-jijg31ym688jpuuc","workflowId":"wfs-jkec6badx3d8e6nn","workflowName":"aaaa","instanceId":"ins-jkedr4cu5mmeii2s","instanceStatus":"SUCCESS"}

可以注意到,设置了通知验证后,在通知请求的header中,有3个headernotification-auth-expire, notification-auth-user, notification-auth-token,这3个header是这样设置的。

notification-auth-user是用户的userId;
notification-auth-expire是发送通知时取的时间戳(并非精准的过期时间,仅用于计算token);
notification-auth-token的计算公式为

content = POST;endpoint;body;notification-auth-expire;notification-auth-user
notification-auth-token = SHA256-HEX(token, content)

endpoint是通知设置里的endpoint, body是通知请求的body, SHA256-HEX是一个Hash算法,详见

为了便于用户开发,下面将给出Java实现的计算notification-auth-token的方法,其他语言版本可以自行实现。

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Hex;

public class Sha256Helper {

    private static final String HMAC_SHA256 = "HmacSHA256";
    private static final Charset CHARSET_UTF8 = StandardCharsets.UTF_8;

    public static String hmacSha256(String secretKey, String input) {
        return hmacSha256(secretKey.getBytes(CHARSET_UTF8), input.getBytes(CHARSET_UTF8));
    }

    public static String hmacSha256(byte[] secretKey, byte[] input) {
        return hmacSha256(secretKey, input, 0, input.length);
    }

    private static String hmacSha256(byte[] secretKey, byte[] input, int offset, int length) {
        return new String(Hex.encodeHex(mac(input, offset, length, new SecretKeySpec(secretKey, HMAC_SHA256))));
    }

    private static byte[] mac(byte[] input, int offset, int length, SecretKey secretKey) {
        try {
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            mac.init(secretKey);
            mac.update(input, offset, length);
            return mac.doFinal();
        } catch (NoSuchAlgorithmException | InvalidKeyException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static void main(String[] args) {
        String endpoint = "http://qwe.com/vw/callback";     // notification endpoint
        String token = "qweASD123";
        String userId = "e95e33a028bd49dbb3e08f068dc975d5"; // notification-auth-user
        String expireTime = "1572923085545";                // notification-auth-expire
        String body = "{\"mediaId\":\"mda-jijg31ym688jpuuc\",\"workflowId\":\"wfs-jkec6badx3d8e6nn\","
                + "\"workflowName\":\"aaaa\",\"instanceId\":\"ins-jkedr4cu5mmeii2s\",\"instanceStatus\":"
                + "\"SUCCESS\"}";                           // request body
        String content = String.format("POST;%s;%s;%s;%s", endpoint, body, expireTime, userId);
        String auth = Sha256Helper.hmacSha256(token, content);
        System.out.println(auth);
    }
}
上一篇
文件上传
下一篇
音视频处理