Newer
Older
huludao / src / main / java / com / newfiber / led / business / util / ContentReviewUtil.java
package com.newfiber.led.business.util;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.tms.v20201229.TmsClient;
import com.tencentcloudapi.tms.v20201229.models.TextModerationRequest;
import com.tencentcloudapi.tms.v20201229.models.TextModerationResponse;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @Description
 * @Auher: Administrator
 * @Data: 2021/11/24 0025 15:53
 */
public class ContentReviewUtil {

    //腾讯云API密钥
    private static final String secretId = "AKIDoOp3B8JLacTElCnM5bbxHOGuHsGOd5NB";
    private static final String secretKey = "lK8xY6MAkm8F6kQr1xkHYJp9EgtpVv87";
    //private static final String secretId = "AKIDQecKlvoQcaHhrjKeiV2HGyPZS5SYNwEF";
    //private static final String secretKey = "lFSO3TdV7SYPzJXUAZhZAUXKr76oWy82";

    //api接口
    private static final String Endpoint = "tms.tencentcloudapi.com";
    //地域参数
    private static final String region = "ap-shanghai";

    public static TextModerationResponse contentReview(String content) throws UnsupportedEncodingException, TencentCloudSDKException {
        //BASE64编码
        byte[] textByte = content.replace(" ", "").getBytes("UTF-8");
        Base64.Encoder encoder = Base64.getEncoder();
        String encodedText = encoder.encodeToString(textByte);

        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(Endpoint);
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        TmsClient client = new TmsClient(cred, region, clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        TextModerationRequest req = new TextModerationRequest();
        req.setContent(encodedText);
        // 返回的resp是一个TextModerationResponse的实例,与请求对象对应
        TextModerationResponse resp = client.TextModeration(req);
        // 内容判断
        return resp;
    }

}