Java调用腾讯会议Api示例

最近疫情严重,公司准备出一个在线视频会议功能,需要调用腾讯会议的API,经过查看腾讯的api文档和与腾讯技术人员交流,终于能成功调用Api接口了!

腾讯会议官方API:https://cloud.tencent.com/document/product/1095/42407

首先第一步需要申请腾讯 API 接入

申请好了以后,会邮件发给你APPID,SecretId,SecretKey三个参数。

我们以创建会议为例,步骤如下:

一、生成body参数

        //body参数
        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 num="";//随机数
String signature = sign(SecretId, SecretKey, "POST", num, createTime, uri, req_body);
        

 三、请求

 String code=doPost(url,Content_Type,SecretId,createTime,num,appId,signature,datas);

完整代码如下:

package com.jbox.common.utils;

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;

/**
 * @author huangting
 * @version 1.0
 * @date 17:43 2020/4/8
 */
public class TencentUtil {
    public static String createMeetings() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String uri="/v1/meetings";
        String SecretId="这里请输入你们公司的SecretId";
        String SecretKey="这里请输入你们公司的SecretKey";
        String appId="这里请输入你们公司的appId";
        String url="https://api.meeting.qq.com/v1/meetings";
        int num=617877739;
        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);
        //body参数
        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, "POST", "617877739", String.valueOf(createTime), uri, req_body);
        System.out.println("我是签名:"+signature);
        String Content_Type="application/json";
        String datas = req_body;
        String code=doPost(url,Content_Type,SecretId,createTime,num,appId,signature,datas);
        System.out.println(code);
        return code;
    }

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

    private static String doPost(String urlPath,String Content_Type,String SecretId,int 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", "application/json");
            conn.setRequestProperty("X-TC-Key", SecretId);
            conn.setRequestProperty("X-TC-Timestamp", String.valueOf(createTime));
            conn.setRequestProperty("X-TC-Nonce", String.valueOf(num));
            conn.setRequestProperty("AppId", appId);
            conn.setRequestProperty("X-TC-Signature", signature);
            System.out.println(signature);
            DataOutputStream out =new DataOutputStream(conn.getOutputStream());
            out.write(datas.getBytes());
            out.flush();
            out.close();
            // 定义BufferedReader输入流来读取URL的响应
            int code = conn.getResponseCode();
            System.out.print("====="+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("调用kettle服务失败:"+";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)));
    }
}

 

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 28
    评论
如果您想在Java中使用腾讯云语音识别API,可以借助腾讯云提供的Java SDK来实现。以下是一些基本的使用步骤: 1. 首先,您需要在腾讯云官网上开通语音识别服务,并获取到相应的SecretID和SecretKey。 2. 接下来,您需要在Java项目中引入腾讯云的Java SDK,可以通过Maven或Gradle等构建工具添加依赖。 3. 在Java代码中,通过创建一个语音识别客户端对象来调用语音识别API。您需要在创建客户端对象时指定API的地域、SecretID和SecretKey等参数,例如: ``` import com.tencentcloudapi.asr.v20190614.AsrClient; import com.tencentcloudapi.asr.v20190614.models.*; AsrClient client = new AsrClient(new Credential("your-secret-id", "your-secret-key"), "ap-guangzhou"); ``` 4. 调用语音识别API进行语音识别,例如: ``` // 创建API请求对象 CreateRecTaskRequest request = new CreateRecTaskRequest(); request.setEngineModelType("16k_zh"); request.setChannelNum(1); request.setResTextFormat(0); request.setDataLen(data.length); request.setData(new String(Base64.encodeBase64(data))); // 发送API请求并获取响应 CreateRecTaskResponse response = client.CreateRecTask(request); // 解析响应结果 if (response != null && response.getData() != null) { String result = new String(Base64.decodeBase64(response.getData())); System.out.println(result); } ``` 以上代码仅是一个简单的示例,具体实现还需要根据您的实际需求进行调整。同时,腾讯云还提供了详细的API文档和示例代码供您参考。
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值