腾讯会议api测试

1,008 阅读4分钟

腾讯会议api测试java版本

package com.test.meeting;

import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; import java.util.Calendar; import java.util.Date;

public class TencentMeetingTest {

private static final String SecretId = "";
private static final String SecretKey = "";
private static final String appId = "";
private static final String num = "123456789";

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

public static String createMeetings() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
    String url = "https://api.meeting.qq.com/v1/meetings";
    String uri = "/v1/meetings";
    String requestMethod = "POST";
    Date today = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DAY_OF_MONTH, 1);
    Date tomorrow = c.getTime();
    int createTime = getSecondTimestamp(today);
    int createtomorrow = getSecondTimestamp(tomorrow);
    String req_body = "{\n" + "\t\"userid\": \"tester\",\n" + "\t\"instanceid\": 1,\n"
            + "\t\"subject\": \"tester's meeting\",\n" + "\t\"type\": 0,\n" + "\t\"hosts\": [{\n"
            + "\t\t\"userid\": \"tester\"\n" + "\t}],\n" + "\t\"start_time\": \"" + createTime + "\",\n"
            + "\t\"end_time\": \"" + createtomorrow + " \",\n" + "\t\"settings \": {\n"
            + "\t\t\"mute_enable_join \": true,\n" + "\t\t\"allow_unmute_self \": false,\n"
            + "\t\t\"mute_all \": false,\n" + "\t\t\"host_video \": true,\n"
            + "\t\t\"participant_video \": false,\n" + "\t\t\"enable_record \": false,\n"
            + "\t\t\"play_ivr_on_leave\": false,\n" + "\t\t\"play_ivr_on_join\": false,\n"
            + "\t\t\"live_url\": false\n" + "\t}\n" + "}";
    String signature = sign(SecretId, SecretKey, requestMethod, num, String.valueOf(createTime), uri, req_body);
    System.out.println("签名:" + signature);
    String Content_Type = "application/json";
    String datas = req_body;
    String code = doHttpRequest(url, Content_Type, SecretId, createTime, num, appId, signature, datas,
            requestMethod);
    System.out.println(code);
    return code;
}

public static String queryMeetings() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
	//String url = "https://api.meeting.qq.com/v1/meetings/7567454748865986567?userid=tester&instanceid=1";
    //String uri = "/v1/meetings/7567454748865986567?userid=tester&instanceid=1";
    String url = "https://api.meeting.qq.com/v1/meetings?meeting_code=123456789&userid=tester&instanceid=1";
    String uri = "/v1/meetings?meeting_code=123456789&userid=tester&instanceid=1";
    String requestMethod = "GET";
    Date today = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DAY_OF_MONTH, 1);
    int createTime = getSecondTimestamp(today);
    String req_body = "";
    String signature = sign(SecretId, SecretKey, requestMethod, num, String.valueOf(createTime), uri, req_body);
    System.out.println("签名:" + signature);
    String Content_Type = null;
    String datas = req_body;
    String code = doHttpRequest(url, Content_Type, SecretId, createTime, num, appId, signature, datas,
            requestMethod);
    System.out.println(code);
    return code;
}

public static String getMeetingMember() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
    String url = "https://api.meeting.qq.com/v1/meetings/7567454748865986567/participants?userid=tester";
    String uri = "/v1/meetings/7567454748865986567/participants?userid=tester";
    String requestMethod = "GET";
    Date today = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DAY_OF_MONTH, 1);
    int createTime = getSecondTimestamp(today);
    String req_body = "";
    String signature = sign(SecretId, SecretKey, requestMethod, num, String.valueOf(createTime), uri, req_body);
    System.out.println("签名:" + signature);
    String Content_Type = null;
    String datas = req_body;
    String code = doHttpRequest(url, Content_Type, SecretId, createTime, num, appId, signature, datas,
            requestMethod);
    System.out.println(code);
    return code;
}

public static String cancelMeetings() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
    String url = "https://api.meeting.qq.com/v1/meetings/7567454748865986567/cancel";
    String uri = "/v1/meetings/7567454748865986567/cancel";
    String requestMethod = "POST";
    Date today = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DAY_OF_MONTH, 1);
    int createTime = getSecondTimestamp(today);
    String req_body = "{\"userid\":\"tester\",\"instanceid\":1,\"reason_code\":1,\"reason_detail\":\"取消会议\"}";
    String signature = sign(SecretId, SecretKey, requestMethod, num, String.valueOf(createTime), uri, req_body);
    System.out.println("签名:" + signature);
    String Content_Type = "application/json";
    String datas = req_body;
    String code = doHttpRequest(url, Content_Type, SecretId, createTime, num, appId, signature, datas,
            requestMethod);
    System.out.println(code);
    return code;
}

public static String changeMeetings() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
    String url = "https://api.meeting.qq.com/v1/meetings/7567454748865986567";
    String uri = "/v1/meetings/7567454748865986567";
    String requestMethod = "PUT";
    Date today = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DAY_OF_MONTH, 1);
    int createTime = getSecondTimestamp(today);
    String req_body = "{\r\n" + 
            "    \"subject\" : \"tester's meeting\",\r\n" + 
            "    \"userid\":\"tester\",\r\n" + 
            "    \"instanceid\":1,\r\n" + 
            "    \"hosts\" : [\"tester\"],\r\n" + 
            "    \"invitees\" : [\"test1\", \"test2\", \"test3\"],\r\n" + 
            "    \"start_time\" : \"2586485885\",\r\n" + 
            "    \"end_time\" : \"2586572286\",\r\n" + 
            "    \"password\" : \"1111\",\r\n" + 
            "    \"settings\" : {\r\n" + 
            "        \"mute_enable_join\":true,\r\n" + 
            "        \"allow_unmute_self\":false,\r\n" + 
            "        \"mute_all\": false,\r\n" + 
            "        \"host_video\":true,\r\n" + 
            "        \"participant_video\": false,\r\n" + 
            "        \"enable_record\": false,\r\n" + 
            "        \"play_ivr_on_leave\": false,\r\n" + 
            "        \"play_ivr_on_join\": false,\r\n" + 
            "        \"live_url\": false\r\n" + 
            "    }\r\n" + 
            "}";
    String signature = sign(SecretId, SecretKey, requestMethod, num, String.valueOf(createTime), uri, req_body);
    System.out.println("签名:" + signature);
    String Content_Type = "application/json";
    String datas = req_body;
    String code = doHttpRequest(url, Content_Type, SecretId, createTime, num, appId, signature, datas,
            requestMethod);
    System.out.println(code);
    return code;
}

public static String getUserMeetingList() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
    String url = "https://api.meeting.qq.com/v1/meetings?userid=tester&instanceid=1";
    String uri = "/v1/meetings?userid=tester&instanceid=1";
    String requestMethod = "GET";
    Date today = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DAY_OF_MONTH, 1);
    int createTime = getSecondTimestamp(today);
    String req_body = "";
    String signature = sign(SecretId, SecretKey, requestMethod, num, String.valueOf(createTime), uri, req_body);
    System.out.println("签名:" + signature);
    String Content_Type = null;
    String datas = req_body;
    String code = doHttpRequest(url, Content_Type, SecretId, createTime, num, appId, signature, datas,
            requestMethod);
    System.out.println(code);
    return code;
}

private static String doHttpRequest(String urlPath, String Content_Type, String SecretId, int createTime,
        String num, String appId, String signature, String datas, String method) throws IOException {
    StringBuilder sub = new StringBuilder();
    try {
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod(method);
        if (!"GET".equals(method)) {
            conn.setDoInput(true);
            conn.setRequestProperty("Content-Type", Content_Type);
        }
        conn.setRequestProperty("X-TC-Key", SecretId);
        conn.setRequestProperty("X-TC-Timestamp", String.valueOf(createTime));
        conn.setRequestProperty("X-TC-Nonce", num);
        conn.setRequestProperty("AppId", appId);
        conn.setRequestProperty("X-TC-Signature", signature);
        conn.connect();
        if (!"".contentEquals(datas)) {
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.write(datas.getBytes());
            out.flush();
            out.close();
        }
        int code = conn.getResponseCode();
        System.out.println("=====" + code);
        System.out.println("=====" + conn.getResponseMessage());
        if (HttpURLConnection.HTTP_OK == code) {
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                sub.append(line);
            }
            in.close();
        }
        return sub.toString();
    } catch (IOException e) {
        sub = new StringBuilder();
        System.out.println(
                "请求失败:" + ";urlPath=" + urlPath + ";data:" + urlPath + "Message:" + e.getMessage());
    }
    return sub.toString();
}

public static int getSecondTimestamp(Date date) {
    if (null == date) {
        return 0;
    }
    String timestamp = String.valueOf(date.getTime());
    int length = timestamp.length();
    if (length > 3) {
        return Integer.valueOf(timestamp.substring(0, length - 3));
    } else {
        return 0;
    }
}

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)));
}

}