【问题记录】09 对接阿里云内容安全机器审核-文本、图片审核,code报错408

一、问题描述

对接阿里云内容安全/机器审核增强版,代码报错408。

二、解决方法

1、查看错误码

408对应:无权限调用该接口。

检查key和secret之后,发现都正常。

检查之后发现,RAM对应的账号未授权对应权限。

2、解决方法:

进入"RAM 访问控制"控制台:用户/权限管理

点击"新增授权",增加权限策略:AliyunYundunGreenWebFullAccess

测试之后,OK。

备注:如果检测不太准确或者缺少一些,还可以去"规则配置"管理检测规则,开启自己需要的规则即可。

三、附:实现对接的代码

pom.xml增加:

java 复制代码
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>green20220302</artifactId>
    <version>3.0.1</version>
</dependency>

实现类AliTmpUtil:

java 复制代码
    private String key = "";
    private String secret = "";
    private String regionId = "cn-shanghai";
    private String endpoint = "green-cip.cn-shanghai.aliyuncs.com";

    private Client createClient() {
        Client client = null;
        try {
            Config config = new Config()
                    .setAccessKeyId(key)
                    .setEndpoint(endpoint)
                    .setRegionId(regionId)
                    .setAccessKeySecret(secret);
            config.setReadTimeout(6000);
            config.setConnectTimeout(3000);
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }

文本审核:

java 复制代码
public boolean textModerationPlus(String content) {
        if (content == null || content.trim().isEmpty()) {
            return false;
        }
        Client client = this.createClient();
        if (client == null) {
            return false;
        }

        JSONObject serviceParameters = new JSONObject();
        serviceParameters.set("content", content);

        TextModerationPlusRequest textModerationPlusRequest = new TextModerationPlusRequest();
        textModerationPlusRequest.setService("chat_detection_pro");
        textModerationPlusRequest.setServiceParameters(serviceParameters.toJSONString(4));

        TextModerationPlusResponse response;
        try {
            response = client.textModerationPlus(textModerationPlusRequest);
        } catch (Exception e) {
            throw new ServiceException("安全审核失败");
        }

        if (response == null || response.getBody() == null) {
            return false;
        }

        TextModerationPlusResponseBody result = response.getBody();
        Integer code = result.getCode();
        if (!Integer.valueOf(200).equals(code)) {
            return false;
        }

        TextModerationPlusResponseBody.TextModerationPlusResponseBodyData data = result.getData();
        if (data == null) {
            return false;
        }

        if ("none".equals(data.getRiskLevel())) {
            return true;
        } else {
            throw new ServiceException("内容包含敏感词");
        }
    }

图片审核:

java 复制代码
public boolean imageModerationPlus(String content) {
        if (content == null || content.trim().isEmpty()) {
            return false;
        }
        
        // 处理多个URL的情况(逗号分隔)
        String[] urls = content.split(",");
        
        Client client = this.createClient();
        if (client == null) {
            return false;
        }

        RuntimeOptions runtime = new RuntimeOptions();

        // 遍历每个URL进行审核
        for (String url : urls) {
            String imageUrl = url.trim();
            if (imageUrl.isEmpty()) {
                continue;
            }

            JSONObject serviceParameters = new JSONObject();
            serviceParameters.set("imageUrl", imageUrl);
            serviceParameters.set("dataId", UUID.randomUUID().toString());

            ImageModerationRequest request = new ImageModerationRequest();
            request.setService("baselineCheck");
            request.setServiceParameters(serviceParameters.toJSONString(4));

            ImageModerationResponse response;
            try {
                response = client.imageModerationWithOptions(request, runtime);
            } catch (Exception e) {
                throw new ServiceException("图片安全审核失败: " + imageUrl);
            }

            if (response == null || response.getBody() == null) {
                return false;
            }

            ImageModerationResponseBody result = response.getBody();
            Integer code = result.getCode();
            if (!Integer.valueOf(200).equals(code)) {
                return false;
            }

            ImageModerationResponseBody.ImageModerationResponseBodyData data = result.getData();
            if (data == null) {
                return false;
            }

            // 只要有一个图片不通过,就返回失败
            if (!"none".equals(data.getRiskLevel())) {
                throw new ServiceException("图片包含违规信息");
            }
        }
        return true;
    }
相关推荐
红姐跨境书12 小时前
从零打造高性能 API 网关: Go 语言中的双缓冲 Dynamic Config 最佳实践
安全
迪康Defender17 小时前
终端安全实战:如何高效解决企业U盘泄密与管控难题
运维·网络·安全·web安全·终端安全管理
网安小学生(兼顾数据库版)17 小时前
固件+软件供应链:ONEKEY+Mend如何实现全链路安全覆盖
网络·安全·web安全
珠海西格电力19 小时前
零碳园区管理系统“智慧大脑”功能对园区运营成本的影响有哪些?
大数据·人工智能·安全·系统架构·能源
发量惊人的中年网工19 小时前
2026年DDoS防护方案怎么选?从攻击响应、清洗位置到成本账单,解析全球高防方案
大数据·网络·安全·ddos
国科安芯20 小时前
抗辐射LDO芯片在星载SAR成像系统中的供电架构应用研究
安全·ldo·成像系统·抗辐射·星载sar
航飞光电市场经理1 天前
UWB定位技术选型指南:从芯片架构到定位引擎的底层能力分析
大数据·人工智能·物联网·安全·人员定位
终端安全笔记1 天前
iOS 27 之后「策略空转」:设备升级不报错,但旧策略不再管它
android·网络·安全·ios·智能手机
鹿鸣天涯1 天前
护网专项工作 | 勒索病毒防护指南
网络·计算机网络·安全
罗斯8391 天前
EMBER恶意软件基准数据集
人工智能·算法·安全·网络安全