美文网首页
Python用requests访问微信小程序security.m

Python用requests访问微信小程序security.m

作者: 骑着蜗牛闯世界666 | 来源:发表于2023-05-13 17:10 被阅读0次

新做的一个小程序需要接入敏感词过滤,因为微信已经提供了这样子的接口服务,所以直接按照微信小程序官方说明进行接入:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html 。但是接入的过程发现了两个很奇葩的问题。
一个是参数格式不合法:{‘errcode’: 47001, ‘errmsg’: ‘data format error rid: xxx’};
一个是解决了参数不合法的问题,所有的敏感词返回的结果全是返回OK,达不到过滤敏感词的目的;
通过API 文档我们知道
1、需要传入headers明确表示 post 的数据类型
2、需要将 post 数据转换为 json 格式
在摸索了一段时间之后,终于知道跟 python 使用的 requeset 版本有关。这里小结记录一下:

    params = {
        "content": content
    }
    headers = {
        "Content-Type": "application/json"
    }
    dataString = json.dumps(params, ensure_ascii=False)
    # 发送 POST 请求
    # 如果 requests 使用的是 2.28.2,则使用这种方式
    #resp = requests.post(post_url, data=dataString.encode("utf-8").decode("latin1"), headers=headers)
    # 如果 requests 使用的是 2.30.0,则使用这种方式
    resp = requests.post(post_url, data=dataString.encode("utf-8"), headers=headers)

相关文章

网友评论

      本文标题:Python用requests访问微信小程序security.m

      本文链接:https://www.haomeiwen.com/subject/dwqusdtx.html