美文网首页
微信公众平台 token 验证失败 python3

微信公众平台 token 验证失败 python3

作者: hope20 | 来源:发表于2020-05-16 13:46 被阅读0次

    官方文档handle.py python2.7

    # -*- coding: utf-8 -*-
    # filename: 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 = "xxxx" #请按照公众平台官网\基本配置中信息填写
    
                list = [token, timestamp, nonce]
                list.sort()
                sha1 = hashlib.sha1()
                map(sha1.update, list)
                hashcode = sha1.hexdigest()
                print "handle/GET func: hashcode, signature: ", hashcode, signature
                if hashcode == signature:
                    return echostr
                else:
                    return ""
            except Exception, Argument:
                return Argument
    
    

    修改如下handle.py python3

    # -*- coding: utf-8 -*-
    # filename: 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 = "xxxx" #请按照公众平台官网\基本配置中信息填写
                list = [token, timestamp, nonce]
                list.sort()
                sha1 = hashlib.sha1()
                sha1.update(list[0].encode("utf-8"))
                sha1.update(list[1].encode("utf-8"))
                sha1.update(list[2].encode("utf-8"))
                hashcode = sha1.hexdigest() #获取加密串
    
    
                print ("handle/GET func: hashcode, signature: ", hashcode, signature)
                if hashcode == signature:
                    return echostr
                else:
                    return ""
            except Exception as Argument:
                return Argument
    
    

    相关文章

      网友评论

          本文标题:微信公众平台 token 验证失败 python3

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