客户首先需要在依图语音开放平台注册并登录,在[接口管理-申请授权]页面申请DevId和DevKey,通过审核后,在[接口管理-申请授权]页面“我的ID”可以查看已有的DevId和DevKey:
DevId
唯一的用户ID, 举例 "10000232"
DevKey
用户密匙, 举例 "^#BCYDEYE#"
在调用任何业务接口前,必须先取得授权,通过认证。取得授权的方式为在HTTP的请求头中输入正确的账号、时间戳及签名(x-dev-id、x-signature、x-request-send-timestamp)。说明如下:
参数名 | 说明 |
---|---|
x-dev-id | 依图语音开放平台分配的DevId。 |
x-request-send-timestamp | 请求发送时刻的时间戳(UTC时间,精确到秒,举例:2018年12月10日9点30分00秒为1544405400)。注意,如果此时间戳与服务端收到请求的时间相差5分钟及以上,则该请求会被判为无效。 |
x-signature | 请求签名,服务端根据请求签名来验证请求的合法性。请求签名的计算方法为HmacSHA256(x-dev-id + x-request-send-timestamp),用DevKey加密。 |
以下为获取x-dev-id、x-signature、x-request-send-timestamp的演示代码:
// api 2.0 校验签名 sample code
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.SignatureException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
public class Signature{
private static final String HMAC_SHA256_ALGORITHM = "HmacSHA256";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String calculateRFC2104HMAC(String data, String key)
throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
{
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA256_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(data.getBytes()));
}
public static void main(String []args) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
String accessId = "x-dev-id";
String accessKey = "x-dev-key";
long timestamp = System.currentTimeMillis() / 1000L;
String signKey = accessId + timestamp;
String signature = calculateRFC2104HMAC(signKey, accessKey);
System.out.println("Send Request With Following Headers");
System.out.println(" x-dev-id: " + accessId);
System.out.println(" x-signature: " + signature);
System.out.println(" x-request-send-timestamp: " + timestamp);
}
}