https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Getting_Started_Guide.html
原有的python2.7版有诸多问题:
1.print问题
2.map函数问题
3.web模块问题
4.认证的接口url 不是/wx 而是 /
在此改为python3+flask
# -*- coding: utf-8 -*-
# 微信公众号接口认证
import hashlib
import json
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def wechat():
if request.method == 'GET':
# 这里改写你在微信公众平台里输入的token
token = 'zhangchuanshuaigejavachuan'
# 获取输入参数
data = request.args
signature = data.get('signature', '')
timestamp = data.get('timestamp', '')
nonce = data.get('nonce', '')
echostr = data.get('echostr', '')
# 字典排序
list = [token, timestamp, nonce]
list.sort()
s = list[0] + list[1] + list[2]
# sha1加密算法
hascode = hashlib.sha1(s.encode('utf-8')).hexdigest()
# 如果是来自微信的请求,则回复echostr
if hascode == signature:
return echostr
else:
return ""
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)