zoukankan      html  css  js  c++  java
  • odoo后台实现微信公众号验证

    在微信公众号开发的其中一个步骤是微信服务器调用我们自己的网站验证身份,这一步微信服务器会传递过来4个参数,可是按照官方的写法,却无法验证通过,下面是官方的验证方法

    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

    网上有网友写的专门的模块,经过实际验证可行,现将这部分的代码单独抽取如下:

        @http.route('/wechat_public_account_auth/validate', type='http', auth="none", methods=["GET"])
        def validate_auth(self, signature, timestamp, nonce, echostr, **kw):
            token = "guoodoo"  # 请按照公众平台官网基本配置中信息填写
            list = [token, timestamp, nonce]
            list_data = []
            for data in list:
                list_data.append(self.to_binary(data))
            list_data.sort()
            _delimiter = self.to_binary(b'')
            str_to_sign = _delimiter.join(list_data)
            hashcode = hashlib.sha1(str_to_sign).hexdigest()
            if hashcode == signature:
                return echostr
            else:
                return ""
    
        def to_binary(self, value, encoding='utf-8'):
            """Convert value to binary string, default encoding is utf-8
            :param value: Value to be converted
            :param encoding: Desired encoding
            """
            if not value:
                return b''
            if isinstance(value, six.binary_type):
                return value
            if isinstance(value, six.text_type):
                return value.encode(encoding)
    
            return self.to_text(value).encode(encoding)
    
        def to_text(self, value, encoding='utf-8'):
            """Convert value to unicode, default encoding is utf-8
            :param value: Value to be converted
            :param encoding: Desired encoding
            """
            if not value:
                return ''
            if isinstance(value, six.text_type):
                return value
            if isinstance(value, six.binary_type):
                return value.decode(encoding)
    
            return six.text_type(value)

    经过比较发现,主要的不同是对token,timestap,nonce字符串进行了编码,代码写好之后,在微信公众平台上填写相关信息进行测试,如下图所示.

    全部源代码可以访问这个地址

    如果通过,微信开放平台会记录下我们的信息,如果失败无法保存。

  • 相关阅读:
    设置发光字
    QQ空间无导航条应对方法
    网页设计经典网站欣赏
    页面居中显示
    获取元素的绝对位置
    输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
    最长重复子字符串
    从头到尾彻底解析Hash 表算法
    求二叉树中节点的最大距离
    MySQL学习笔记——显示数据库信息
  • 原文地址:https://www.cnblogs.com/crazyguo/p/9780657.html
Copyright © 2011-2022 走看看