码迷,mamicode.com
首页 > 微信 > 详细

微信公众号开发-遇到的坑

时间:2018-05-16 15:08:49      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:http   byte   编码   基本   and   upd   类型   time   定义   

在配置后端服务器时,报错 "系统发生错误,请稍后重试"

情景:配置如下截图:
技术分享图片

按照要求使用http标准80端口,但是提交就报错。在服务端抓包,根本没收到请求。那这个报错就是微信公众平台没有发送过来呀。
折腾了半个小时!
我去,发现不能在url中指定80端口,就可以成功,如下图:
技术分享图片
这样不指定端口才正确。微信说明还是不是很明确

在handle模块,实例代码是py2代码,py3中要进行编码转换

  • 微信开发文档的代码,在py3中执行会一直报token验证错误。
# -*- 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)  # 这里list中的字符串在py2中是符合sha1.update要求的
            hashcode = sha1.hexdigest()
            print "handle/GET func: hashcode, signature: ", hashcode, signature
            if hashcode == signature:
                return echostr
            else:
                return ""
        except Exception, Argument:
            return Argument
  • py3修改后的
"""
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

微信公众号开发-遇到的坑

标签:http   byte   编码   基本   and   upd   类型   time   定义   

原文地址:https://www.cnblogs.com/ZJiQi/p/9045413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!