云函数调用msgSecCheck接口报错
发布于 6 年前 作者 mingtang 6984 次浏览 来自 问答

-本地调试时报错:module.exports.ReadError: incorrect header check

云端上调试也是这个报错:{“errorCode”:1,“errorMessage”:“user code exception caught”,“stackTrace”:“incorrect header check”}

  • 确认了我的access_token没有问题,依赖库全部装上,

同样的函数,换了别的接口,对接成功,换回’https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" 就依然不行

然后我就用postman测试https://api.weixin.qq.com/wxa/msg_sec_check?access_token=接口,对接成功,我复制了对接成功的POST地址放回云函数再次调试,依然报错,这是为啥?

开发工具版本是最新的 v1.02.1906062

-大佬们能给看看嘛,救救孩子

云函数的代码

// 云函数入口文件
const cloud = require('wx-server-sdk');
const got=require('got');
 
var appid ='appid';
var appsecret ='appsecret ';
cloud.init();
 
// 拼接 access_token 
let tokenUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appid + '&secret=' + appsecret
// 内容检测接口
let checkUrl = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='
 
// 云函数入口函数
exports.main = async (event, context) => {
  let tokenResponse = await got(tokenUrl); 
  let token = JSON.parse(tokenResponse.body).access_token; 
  
  let checkResponse = await got(checkUrl + token, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      content: event.content
    })
  });
  return checkResponse.body;
}
3 回复
//云函数声明
// 云函数入口文件
 
const cloud = require('wx-server-sdk')
 
 
 
cloud.init()
 
 
 
// 云函数入口函数
 
exports.main = async (event, context) => {
 
try {
 
const checkmsg = await cloud.openapi.security.msgSecCheck({
 
content: event.content
 
});
 
return checkmsg;
 
} catch (err) {
 
throw err
 
}
 
}
 
 
//在你需要用到的地方写一个函数调用云函数,也可以直接用
//检测敏感词汇
  msgSecCheck:function(event){
    var str = event
    wx.cloud.callFunction({
      name:'checkMsg',
      data:{
        content:str
      }
    }).then(res=>{
      this.setData({
        checkCode: JSON.stringify(res.result.errCode)
      });
    }).catch(err=>{
      this.setData({
        checkCode: String(err.errCode)
      });
       
    });
  }

我是新手,所以这种写法比较粗糙简陋,用来做个毕业设计就足够了

云函数:

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
try {
const res = await cloud.openapi.security.imgSecCheck({
media: {
contentType: 'image/png',
value: event.img
           }
})
return res;
} catch (err) {
return err;
}

}


本地函数:


wx.chooseImage({count: 1}).then((res) => {
if(!res.tempFilePaths[0]){
return;
}
if (res.tempFiles[0] && res.tempFiles[0].size > 1024 * 1024) {
wx.showToast({
title: '图片不能大于1M',
icon: 'none'
       })
return;
}
wx.request({url:res.tempFilePaths[0],responseType: 'arraybuffer'}).then(
buffer=>{
wx.cloud.callFunction({
name: 'checkImg',
data: {
img:buffer.data
               }
}).then(
imgRes=>{
console.log(JSON.stringify(imgRes))
}
)
}
)
})
回到顶部