微信公众号-后台开发

在公众号->基本配置中->服务器配置中,按照微信文档操作,会一直出现token错误

原因除了服务器配置问题外,还有可能出现在微信文档中的handel.py文件中

在百度中发现https://www.cnblogs.com/ZJiQi/p/9045413.html该解决方法

将代码如此修改:

"""
handle.py
"""
import hashlib
import web


class Handle(object):
    def GET(self):
        try:
            data = web.input()
            if len(data) == 0:
                return "hello, this is handle view"
            signature = data.signature
            timestamp = data.timestamp
            nonce = data.nonce
            echostr = data.echostr
            token = "****"   # 自己定义的tokent

            list = [token, timestamp, nonce]
            list.sort()
            sha1 = hashlib.sha1()
            sha1.update(''.join(list).encode('utf-8'))  # 将py3中的字符串编码为bytes类型
            hashcode = sha1.hexdigest()
            print("handle/GET func: hashcode, signature:", hashcode, signature)
            if hashcode == signature:
                return echostr
            else:
                return ""
        except Exception as e:
            print(e)


if __name__ == '__main__':
    pass

之后提交就可以了

posted @ 2020-09-02 12:01  _Miao  阅读(299)  评论(0)    收藏  举报