首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >腾讯会议API 签名,为何报签名失败?

腾讯会议API 签名,为何报签名失败?

提问于 2020-04-13 13:45:48
回答 3关注 1查看 1.5K

官方给的是java版本的

代码语言:javascript
复制
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
byte[] keyBytes = secret.getBytes("UTF-8");
hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));
String hexString = byteArrayToHex(stringToSign.getBytes("UTF8"));
String sign = new String(Base64.encodeBase64(hmacSha256.doFinal(hexString),"UTF-8"));

我要用javascript 所以改写了下

代码语言:javascript
复制
const secretId = 'xxx';
const secretKey = 'xxx';
const params = {
      userid: 'xx',
      instanceid: 1,
      subject: subject,
      type: 0,
      start_time: getTime(startTime),
      end_time: getTime(endTime),
      settings: {}
};

const HTTPMethod = 'POST';
const url = '/v1/meetings';

const headerString = 'X-TC-Key=' + secretId + '&X-TC-Nonce=' + randomNumber(10000, 99999) + '&X-TC-Timestamp=' + nowTime();
const stringToSign = HTTPMethod + '\n' +
      headerString + '\n' +
      url + '\n' +
      JSON.stringify(params);
console.log(stringToSign);
const key = CryptoJS.enc.Utf8.parse(secretKey);
const content = CryptoJS.enc.Utf8.parse(stringToSign);
const hmac = CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA256(content, key));
console.log(btoa(hmac));

2边生成出来的签名都是一样的,但在postman上测试 还是报 Signature failed! API 请求头都是按照官方给的设置。

请问有使用腾讯会议API的大大们,问题还会出在哪里?谢谢

附上官方JAVA的测试版本代码,同样也是报签名失败。

代码语言:js
复制
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class test{
     public static void main(String []args) throws NoSuchAlgorithmException, InvalidKeyException{
        String headerString = "X-TC-Key=xxx&X-TC-Nonce=84578&X-TC-Timestamp=1586765524";
        String HTTPMethod = "GET";
        String requestUri = "https://api.meeting.qq.com/v1/meetings?userid=xxx&instanceid=1";
        String stringToSign = HTTPMethod + "\n" + headerString  + "\n" + requestUri + "\n" + "";
        System.out.println(stringToSign);                      
        String secretKey = "xxx";
        Mac mac = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), mac.getAlgorithm());
        System.out.println(secretKeySpec);
        mac.init(secretKeySpec);
        byte[] hash = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
        System.out.println(hash);   
        char[] buf = new char[hash.length * 2];
        char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        int index = 0;
        for (byte b : hash) {
          buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
          buf[index++] = HEX_CHAR[b & 0xf];
        }
  
        String hexHash = new String(buf);
        System.out.println(hexHash);
        System.out.println(new String(Base64.getEncoder().encode(hexHash.getBytes(StandardCharsets.UTF_8))));
     }
}

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档