Python 签名示例
#!/usr/bin/python#coding=utf-8import jwtAppId = 1255566655 #用户 appidFileId = "4564972818519602447" #目标 FileIdAudioVideoType = "RawAdaptive" #播放的音视频类型RawAdaptiveDefinition = 10 #允许输出的未加密的自适应码流模板 IDImageSpriteDefinition = 10 #做进度条预览的雪碧图模板 IDCurrentTime = 1546340400PsignExpire = 1546344000 #可任意设置过期时间UrlTimeExpire = "5c2b5640" #可任意设置过期时间PlayKey = "TxtyhLlgo7J3iOADIron"Original = {"appId": AppId,"fileId": FileId,"contentInfo": {"audioVideoType": AudioVideoType,"rawAdaptiveDefinition": RawAdaptiveDefinition,"imageSpriteDefinition": ImageSpriteDefinition},"currentTimeStamp": CurrentTime,"expireTimeStamp": PsignExpire,"urlAccessInfo": {"t": UrlTimeExpire}}Signature = jwt.encode(Original, PlayKey, algorithm='HS256')print("Original: ", Original)print("Signature: ", Signature)
Java 签名示例
import java.util.*;import com.auth0.jwt.algorithms.Algorithm;import com.auth0.jwt.exceptions.JWTCreationException;import com.auth0.jwt.JWT;class Main {public static void main(String[] args) {Integer AppId = 1255566655; // 用户 appidString FileId = "4564972818519602447"; // 目标 FileIdString AudioVideoType = "RawAdaptive"; // 播放的音视频类型Integer RawAdaptiveDefinition = 10; // 允许输出的未加密的自适应码流模板 IDInteger ImageSpriteDefinition = 10; // 做进度条预览的雪碧图模板 IDInteger CurrentTime = 1589448067;Integer PsignExpire = 1589548067; // 可任意设置过期时间String UrlTimeExpire = "5ebe9423"; // 可任意设置过期时间String PlayKey = "TxtyhLlgo7J3iOADIron";HashMap<String, Object> urlAccessInfo = new HashMap<String, Object>();urlAccessInfo.put("t", UrlTimeExpire);HashMap<String, Object> contentInfo = new HashMap<String, Object>();contentInfo.put("audioVideoType", AudioVideoType);contentInfo.put("rawAdaptiveDefinition", RawAdaptiveDefinition);contentInfo.put("imageSpriteDefinition", ImageSpriteDefinition);try {Algorithm algorithm = Algorithm.HMAC256(PlayKey);String token = JWT.create().withClaim("appId", AppId).withClaim("fileId", FileId).withClaim("contentInfo", contentInfo).withClaim("currentTimeStamp", CurrentTime).withClaim("expireTimeStamp", PsignExpire).withClaim("urlAccessInfo", urlAccessInfo).sign(algorithm);System.out.println("token:" + token);} catch (JWTCreationException exception) {// Invalid Signing configuration / Couldn't convert Claims.}}}
Go 签名示例
package mainimport ("fmt""time""strconv""github.com/dgrijalva/jwt-go")func main() {appId := 1255566655 // 用户 appidfileId := "4564972818519602447" // 目标 FileIdaudioVideoType := "RawAdaptive" // 播放的音视频类型rawAdaptiveDefinition := 10 // 允许输出的未加密的自适应码流模板 IDimageSpriteDefinition := 10 // 做进度条预览的雪碧图模板 IDcurrentTime := time.Now().Unix()psignExpire := currentTime + 3600 // 可任意设置过期时间,示例1hurlTimeExpire := strconv.FormatInt(psignExpire, 16) // 可任意设置过期时间,16进制字符串形式,示例1hplayKey := []byte("TxtyhLlgo7J3iOADIron")// Create a new token object, specifying signing method and the claims// you would like it to contain.token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{"appId": appId,"fileId": fileId,"contentInfo": {"audioVideoType": audioVideoType,"rawAdaptiveDefinition": rawAdaptiveDefinition,"imageSpriteDefinition": imageSpriteDefinition,},"currentTimeStamp": currentTime,"expireTimeStamp": psignExpire,"urlAccessInfo": map[string]string{"t": urlTimeExpire,},})// Sign and get the complete encoded token as a string using the secrettokenString, err := token.SignedString(playKey)fmt.Println(tokenString, err)}
C# 签名示例
using System;using System.Text;using System.Collections.Generic;using Jose;public class Program{public static void Main(){var appId = 1255566655; // 用户 appidvar fileId = "4564972818519602447"; // 目标 FileIdvar audioVideoType = "RawAdaptive"; // 播放的音视频类型var rawAdaptiveDefinition = 10; // 允许输出的未加密的自适应码流模板 IDvar imageSpriteDefinition = 10; // 做进度条预览的雪碧图模板 IDvar currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();var psignExpire = currentTime + 3600; // 可任意设置过期时间,示例1hvar urlTimeExpire = psignExpire.ToString("X4"); // 可任意设置过期时间,16进制形式,示例1hvar playKey = "TxtyhLlgo7J3iOADIron";var playKeyBytes = Encoding.ASCII.GetBytes(playKey);var payload = new Dictionary<string, object>(){{"appId", appId},{"fileId", fileId},{"contentInfo", new Dictionary<string, object>(){{"audioVideoType", audioVideoType},{"rawAdaptiveDefinition", rawAdaptiveDefinition},{"imageSpriteDefinition", imageSpriteDefinition}}},{"currentTimeStamp", currentTime},{"expireTimeStamp", psignExpire},{"urlAccessInfo", new Dictionary<string, object>(){{"t", urlTimeExpire}}}};string token = Jose.JWT.Encode(payload, playKeyBytes, JwsAlgorithm.HS256);Console.WriteLine(token);}}
PHP 签名示例
<?phprequire 'vendor/autoload.php';use \\Firebase\\JWT\\JWT;$appId = 1255566655; // 用户 appid$fileId = "4564972818519602447"; // 目标 FileId$audioVideoType = "RawAdaptive"; // 播放的音视频类型$rawAdaptiveDefinition = 10; // 允许输出的未加密的自适应码流模板 ID$imageSpriteDefinition = 10; // 进度条预览的雪碧图模板 ID$currentTime = time();$psignExpire = $currentTime + 3600; // 可任意设置过期时间,示例1h$urlTimeExpire = dechex($psignExpire); // 可任意设置过期时间,16进制字符串形式,示例1h$playKey = "TxtyhLlgo7J3iOADIron";$payload = array("appId" => $appId,"fileId" => $fileId,"contentInfo" => array("audioVideoType"=> $audioVideoType,"rawAdaptiveDefinition"=> $rawAdaptiveDefinition,"imageSpriteDefinition"=> $imageSpriteDefinition),"currentTimeStamp" => $currentTime,"expireTimeStamp" => $psignExpire,"urlAccessInfo" => array("t" => $urlTimeExpire));$jwt = JWT::encode($payload, $playKey, 'HS256');print_r($jwt);?>
Node.js 签名示例
var jwt = require('jsonwebtoken');var appId = 1255566655 // 用户 appidvar fileId = "4564972818519602447" // 目标 FileIdvar audioVideoType = "RawAdaptive" // 播放的音视频类型var rawAdaptiveDefinition = 10 // 允许输出的未加密的自适应码流模板 IDvar imageSpriteDefinition = 10 // 做进度条预览的雪碧图模板 IDvar currentTime = Math.floor(Date.now()/1000)var psignExpire = currentTime + 3600 // 可任意设置过期时间,示例1hvar urlTimeExpire = psignExpire.toString(16) // 可任意设置过期时间,16进制字符串形式,示例1hvar playKey = 'TxtyhLlgo7J3iOADIron'var payload = {appId: appId,fileId: fileId,contentInfo: {audioVideoType: audioVideoType,rawAdaptiveDefinition: rawAdaptiveDefinition,imageSpriteDefinition: imageSpriteDefinition},currentTimeStamp: currentTime,expireTimeStamp: psignExpire,urlAccessInfo: {t: urlTimeExpire}}var token = jwt.sign(payload, playKey);console.log(token);