首先开始写的时候遇到的问题:
Uncaught (in promise) Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID xxxxxxxxxxxxxx, cloud function service error code -504002, error message incorrect header check; at cloud.callFunction api;
网上搜到的很多类似的写法:
exports.main = async (event, context) => {
const tokenRes = await got(tokenUrl)
const token = JSON.parse(tokenRes.body).access_token //拿到token
const res = await got.post(checkUrl+token, {
'Content-Type': 'application/json',
body: JSON.stringify({
content: event.message
})
})
return res.body
}
问题查找了很久,不知道为什么就是一直有问题,最终没有找到问题的原因,事实上通过postman是可以得到检测结果的。
最终,看了官方文档云调用的部分,其实敏感信息验证的接口是可以通过云调用完成验证的,
实现代码如下
// 云函数入口函数
exports.main = async (event, context) => {
const result = await cloud.openapi.security.msgSecCheck({
content: event.text
});
return result;
}
调用部分
//内容合规性检查
wx.cloud.callFunction({
name: 'msgSecCheck',
data: {
text: '这是没有问题的文字'
}
}).then(res => {
console.log(res);
}).catch(console.err);
最终结果
验证一个包含敏感信息的结果如下:
text: '有违规文字内容测试特3456书yuuo莞6543李zxcz蒜7782法fgnv级'
符合errCode:87014