火山引擎内容定制API会对每个访问的请求进行身份验证,因此您需要在请求中包含签名信息。
该文档主要介绍了火山引擎侧内容定制API的签名过程,并提供java
、python
、php
等多种开发语言的签名示例,以便客户侧接入。
当客户在火山引擎内容定制控制台创建正式应用后,进入应用详情页,可以查看App_key,即如下签名方法中的secure_key
参数。
客户在请求火山引擎侧内容定制API时,需要携带公共参数外,还需要遵循如下规则进行签名,否则访问会被拦截,导致请求失败。
注意
如果是wap注册接口,需要将secure_key、timestamp、nonce、 uuid四个参数进行字典序排序
def signature_gen_for_wap_token(secure_key, timestamp, nonce, uuid): #所有参数应该转化为string keys = [secure_key, timestamp,nonce, uuid] keys.sort() keyStr = ''.join(keys) signature = hashlib.sha1(keyStr).hexdigest() return signature
function signature_gen($secure_key, $timestamp, $nonce,$uuid) { $keys=array($secure_key,$timestamp,$nonce,$uuid); sort($keys,2); $keyStr = implode('',$keys); return sha1($keyStr); }
package com.company; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Random; public class Main { // 注册接口生成signature,uuid需要参与计算 public static String signatureGen(String secureKey, String timestamp, String nonce, String uuid) { String[] keys = new String[]{secureKey, timestamp, nonce, uuid}; // 将传入的参数按照ASCII字典序排序 Arrays.sort(keys); // 将排序后的参数拼接在一起,注意无空格 String keyStr = String.join("", keys); // 将拼接后的字符串进行sha-1加密,并转化为十六进制字符串返回 return sha1Hex(keyStr); } // SHA-1加密并转十六进制 public static String sha1Hex(String keyStr) { try { MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] byteArray = keyStr.getBytes(StandardCharsets.UTF_8); byte[] md5Bytes = sha.digest(byteArray); StringBuilder hexValue = new StringBuilder(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { // secureKey 对应火山引擎内容定制控制台,应用详情页App_key String secureKey = "xxx"; // 获取10位秒级时间戳 String timestamp = String.valueOf(System.currentTimeMillis() / 1000); // 获取一个随机字符串 Random random = new Random(); String nonce = String.valueOf(random.nextInt()); // 获取待注册用户的身份标示uuid String uuid = "user_123456"; String signature = signatureGen(secureKey, timestamp, nonce, uuid); System.out.println(secureKey + ", " + timestamp + ", " + nonce + ", " + uuid); System.out.println(signature); } }
import hashlib def signature_gen(secure_key, timestamp, nonce): #所有参数应该转化为string keys = [secure_key, timestamp,nonce]// 将传入的三个参数按照ASCII字典序排序 keys.sort() keyStr = ''.join(keys) //将排序后的三个参数拼接在一起,注意无空格 //将拼接后的字符串进行sha-1加密,并转化为十六进制字符串返回 signature = hashlib.sha1(keyStr).hexdigest() return signature
function signature_gen($secure_key, $timestamp, $nonce) { $keys = array($secure_key, $timestamp, $nonce); sort($keys,2); $keyStr = implode('',$keys); return sha1($keyStr); }
package com.company; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Random; public class Main { public static String signatureGen(String secureKey, String timestamp, String nonce) { String[] keys = new String[]{secureKey, timestamp, nonce}; // 将传入的三个参数按照ASCII字典序排序 Arrays.sort(keys); // 将排序后的三个参数拼接在一起,注意无空格 String keyStr = String.join("", keys); // 将拼接后的字符串进行sha-1加密,并转化为十六进制字符串返回 return sha1Hex(keyStr); } // SHA-1加密并转十六进制 public static String sha1Hex(String keyStr) { try { MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] byteArray = keyStr.getBytes(StandardCharsets.UTF_8); byte[] md5Bytes = sha.digest(byteArray); StringBuilder hexValue = new StringBuilder(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { // secureKey对应火山引擎内容定制控制台,应用详情页App_key String secureKey = "xxx"; // 获取10位秒级时间戳 String timestamp = String.valueOf(System.currentTimeMillis() / 1000); // 获取一个随机字符串 Random random = new Random(); String nonce = String.valueOf(random.nextInt()); String signature = signatureGen(secureKey, timestamp, nonce); System.out.println(secureKey + ", " + timestamp + ", " + nonce); System.out.println(signature); } }