首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >java调用腾讯会议API总是报400?

java调用腾讯会议API总是报400?

提问于 2020-07-16 11:52:14
回答 2关注 0查看 681
代码语言:java
复制
public static String createUsers() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
    String uri="/v1/users";
    String SecretId="";
    String SecretKey="";
    String appId="";
    String url="https://api.meeting.qq.com/v1/users";
    int num = 95279280;
    String  timestamp = Long.toString(System.currentTimeMillis()/1000L);
    //body参数
    String req_body=
                "{\n"+
                "\t\"username\": \"bX9280\", \n"+
                "\t\"phone\" : \"13343411234\", \n"+
                "\t\"userId\": \"bu9280\", \n" +
                "\t\"email\": \"7392615@qq.com\n" +
                "}";
    //生成签名
    String signature = sign(SecretId, SecretKey, "POST", Integer.toString(num), timestamp, uri, req_body);
    String Content_Type="application/json";
    String code = doPost(url,Content_Type,SecretId,timestamp,num,appId,signature, req_body);
    return code;
}

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
    String code = createUsers();
}

private static String doPost(String urlPath,String Content_Type,String SecretId,String createTime,int num,String appId,String signature,String datas) throws IOException {
    StringBuilder sub = new StringBuilder();
    // 建立连接
    try {
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // 设置连接请求属性post
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // 忽略缓存
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", Content_Type);
        conn.setRequestProperty("X-TC-Key", SecretId);
        conn.setRequestProperty("X-TC-Timestamp", createTime);
        conn.setRequestProperty("X-TC-Nonce", String.valueOf(num));
        conn.setRequestProperty("AppId", appId);
        conn.setRequestProperty("X-TC-Signature", signature);
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.write(datas.getBytes(StandardCharsets.UTF_8));
        out.flush();
        out.close();
        // 定义BufferedReader输入流来读取URL的响应
        int code = conn.getResponseCode();
        System.out.println("====="+conn.getResponseMessage()+"-"+conn.getResponseCode());
        if (HttpURLConnection.HTTP_OK == code) {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), StandardCharsets.UTF_8));
            String line;
            while ((line = in.readLine()) != null) {
                sub.append(line);
            }
            in.close();
        }
        return sub.toString();
    } catch (IOException e) {
        e.printStackTrace();
        sub = new StringBuilder();
        System.out.println("调用kettle服务失败:"+";urlPath="+urlPath+";data:"+urlPath+"Message:"+e.getMessage());
    }
    return sub.toString();
}
private static String HMAC_ALGORITHM = "HmacSHA256";
private static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
static String bytesToHex(byte[] bytes) {

    char[] buf = new char[bytes.length * 2];
    int index = 0;
    for (byte b : bytes) {
        buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
        buf[index++] = HEX_CHAR[b & 0xf];
    }
    return new String(buf);
}

/**
 * 生成签名,开发版本oracle jdk 1.8.0_221
 *
 * @param secretId        邮件下发的secret_id
 * @param secretKey       邮件下发的secret_key
 * @param httpMethod      http请求方法 GET/POST/PUT等
 * @param headerNonce     X-TC-Nonce请求头,随机数
 * @param headerTimestamp X-TC-Timestamp请求头,当前时间的秒级时间戳
 * @param requestUri      请求uri,eg:/v1/meetings
 * @param requestBody     请求体,没有的设为空串
 * @return 签名,需要设置在请求头X-TC-Signature中
 * @throws NoSuchAlgorithmException e
 * @throws InvalidKeyException      e
 */
static String sign(String secretId, String secretKey, String httpMethod, String headerNonce, String headerTimestamp, String requestUri, String requestBody)
        throws NoSuchAlgorithmException, InvalidKeyException {
    String tobeSig =
            httpMethod + "\nX-TC-Key=" + secretId + "&X-TC-Nonce=" + headerNonce + "&X-TC-Timestamp=" + headerTimestamp + "\n" + requestUri + "\n" + requestBody;
    Mac mac = Mac.getInstance(HMAC_ALGORITHM);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), mac.getAlgorithm());
    mac.init(secretKeySpec);
    byte[] hash = mac.doFinal(tobeSig.getBytes(StandardCharsets.UTF_8));
    String hexHash = bytesToHex(hash);
    return new String(Base64.getEncoder().encode(hexHash.getBytes(StandardCharsets.UTF_8)));
}

回答

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

相似问题

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