Posts
小程序内容安全
小程序内容安全
疫情后在小公司实习的任务之一
API文档 检测图片或文本内容是否违规
HTTP调用
调用需获取全局唯一后台接口调用凭据ACCESS_TOKEN
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
请求参数为小程序appID与密钥appSecret
img
小于1M的图片高分辨率也能返回识别结果, 图片大小大于1M的不行
def img_test(img, ACCESS_TOKEN='') -> dict:
# img file type support PNG,JPEG,JPG,GIF, img size boundary: 750px x 1334px
url = f'https://api.weixin.qq.com/wxa/img_sec_check?access_token={ACCESS_TOKEN}'
data = {'media': open(img, 'rb').read()}
headers = {'Content-Type': 'multipart/form-data'}
res = requests.post(url, files=data, headers=headers)
print(res.json())
return res.json()msg
def content_test(msg, ACCESS_TOKEN='') -> dict:
url = f'https://api.weixin.qq.com/wxa/msg_sec_check?access_token={ACCESS_TOKEN}'
data = {"content": msg}
data = json.dumps(data).encode('utf-8')
headers = {'Content-Type': 'application/json'}
res = requests.post(url, data=data, headers=headers)
print(res.json())import requests
import os
import hashlib
encrypt = hashlib.md5
def content_check(content, ACCESS_TOKEN):
digest = encrypt(content).hexdigest()
# 255216 jpg; 7173 gif; 6677 BMP, 13780 PNG; 7790 exe, 8297 rar
file_info = str(content[0]) + str(content[1])
fmt = 'https://api.weixin.qq.com/wxa/{}_sec_check?access_token=' + ACCESS_TOKEN
# img content
if file_info in ['255216', '7173', '6677', '13780']:
url = fmt.format('img')
# content: bytes of raw img
if(len(content) > 1024 * 1024):
pass # resize
files = {"media": content}
headers = {'Content-Type': 'multipart/form-data'}
res = requests.post(url, files=files, headers=headers)
else:
# msg content
url = fmt.format('msg')
# content: bytes of str dict
if(len(content) > 500 * 1024):
pass # slice
headers = {'Content-Type': 'application/json'}
res = requests.post(url, data=content, headers=headers)
return res.json()