官方文档
获取凭证access_token
此处演示为python代码,请求接口
https://api.weixin.qq.com/cgi-bin/token
请求方式:GET
参数:
appid | 小程序的appid |
secret | 小程序的secret |
import requests
appid = "APPID"
secret = "SECRET"
url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}".format(appid, secret)
response = eval(requests.get(url).content.decode())
access_token = response["access_token"]
文本内容安全识别
请求接口url,注意将上一步所获取的access_token拼接到url后面
https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN
请求方式:POST
参数:
content | 需检测的文本内容,文本字数的上限为2500字,需使用UTF-8编码 |
version | version |
scene | 场景枚举值(1 资料;2 评论;3 论坛;4 社交日志) |
openid | 场景枚举值(1 资料;2 评论;3 论坛;4 社交日志) |
title | 非必要 |
nickname | 非必要 |
signature | 非必要 |
import requests
import json
url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token={}".format(access_token)
openid = "OPENID"
headers = {
"Content-Type": "application/json",
}
data = {
"content": "hello",
"version": 2,
"scene": 1,
"openid": openid,
}
response = requests.post(url, headers=headers, data=json.dumps(data)).content.decode()
print(response)
这里我们的字典参数需要转为json数据格式,使用json.dumps()方法,否则会返回47001错误。
{"errcode":47001,"errmsg":"data format error rid: 63d92619-73b5d40b-5992506d"}
正确返回数据
{
"errcode":0,
"errmsg":"ok",
"detail":[
{"strategy":"keyword","errcode":0},{"strategy":"content_model","errcode":0,"suggest":"pass","label":100,"prob":90}
],
"trace_id":"63d926cf-724c4586-7bb97be9",
"result":{"suggest":"pass","label":100}
}
源码
替换代码中的三个参数appid、secret、openid即可。
import requests
import json
# 获取凭证access_token
appid = "APPID"
secret = "SECRET"
url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}".format(appid, secret)
response = eval(requests.get(url).content.decode())
access_token = response["access_token"]
# 文本内容安全识别
url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token={}".format(access_token)
openid = "OPENID"
headers = {
"Content-Type": "application/json",
}
data = {
"content": "hello",
"version": 2,
"scene": 1,
"openid": openid,
}
response = requests.post(url, headers=headers, data=json.dumps(data)).content.decode()
print(response)
Comments NOTHING