微信小程序敏感內容檢測


獲取access_token

access_token是公眾號的全局唯一接口調用憑據,公眾號調用各接口時都需使用access_token。開發者需要進行妥善保存。access_token的存儲至少要保留512個字符空間。access_token的有效期目前為2個小時,需定時刷新,重復獲取將導致上次獲取的access_token失效。

在使用敏感文本接口和敏感圖片接口都需要access_token參數,獲取access_token接口為

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

| 參數 | 是否必須 | 說明 |

| grant_type | 是 | 此處為client_credential |

| appid | 是 | 小程序的appid |

| secret | 是 | 小程序的appsecret |

正常返回結果

{"access_token":"ACCESS_TOKEN","expires_in":7200}

其他具體信息查看文檔

敏感文本檢測

這是接口基於https協議。開發者服務器可以調用此接口校驗一段文本是否含有敏感信息。接口為

https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN

| 參數 | 是否必須 | 說明 |

| access_token | 是 | 接口憑證 |

| content | 是 | 檢測的文本內容 |

正常返回結果

{
    "errcode": "0",
    "errmsg": "ok"
}

當content內含有敏感信息,則返回87014

{
    "errcode": 87014,
    "errmsg": "risky content"
}

其余錯誤見返回碼說明

{
    "errcode": 40001,
    "errmsg": "invalid credential, access_token is invalid or not latest"
}

示例代碼

wx.request({
    url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=your app id&secret=your secret',
    method: 'GET',
    success: res => {
        var access_token = res.data.access_token;
        wx.request({
            method: 'POST',
            url: `https://api.weixin.qq.com/wxa/msg_sec_check?access_token=${access_token}`,
            data: {
                content: me.data.title
            },
            success(res) {
                if (res.errcode !== 87014) {
                    // 合格
                }
            }
        })
    },
    fail() {
        console.log(res);
    }
})

敏感圖片檢測

這是接口基於HTTPS協議。開發者服務器可以調用此接口校驗一張圖片是否含有敏感信息。接口為

https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN

| 參數 | 是否必須 | 說明 |

| access_token | 是 | 接口憑證 |

| media | 是 | 圖片文件,支持jpeg,jpg,png,gif,像素不超過750*1334 |

正常返回結果

{
    "errcode": "0",
    "errmsg": "ok"
}

當圖片文件內含有敏感內容,則返回87014

{
    "errcode": 87014,
    "errmsg": "risky content"
}

其余錯誤見返回碼說明

{
    "errcode": 40001,
    "errmsg": "invalid credential, access_token is invalid or not latest"
}

在使用圖片接口時候,如以下示例

let formData = new FormData();
formData.append('file', file);
wx.request({
  url: `https://api.weixin.qq.com/wxa/img_sec_check?access_token=${access_token}`,
  method: 'POST',
  data: {
    media: formData
  },
  success: res => {
    console.log(res);
  }
})

發現報錯,百度了都說要PHP什么鬼

{"errcode":41005,"errmsg":"media data missing hint: [UQNXoA04384524]"}

最后發現解決方法是提交文件時候設置header頭部信息'Content-Type': 'application/octet-stream',所以在請求的頭部添加header配置即可

wx.request({
    url: `https://api.weixin.qq.com/wxa/img_sec_check?access_token=${access_token}`,
    method: 'POST',
    header: {
        'Content-Type': 'application/octet-stream'
    },
    data: {
        media: formData
    },
    success: res => {
        console.log(res);  // {"errcode":0,"errmsg":"ok"}
    }
})

轉載自:https://funteas.com/topic/5b0926d0a9f4524666d3109c


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM