公众号快速部署。 二话不说上源码,
首先需要以下资源: 服务器url (这里用的是新浪SAE ,不会部署的教程 链接在此 http://blog.csdn
首先,需要写 flask 后台代码,以下以一个简单的后台做为示范。
将如下代码部署到 新浪SAE服务器。注意:这里代码里面的token 值,这个值可填任意值。但是要记住,一会微信要用。
# -*- coding=utf-8 -*-
import time
from flask import Flask,g,request,make_response
import hashlib
import xml.etree.ElementTree as ET
app = Flask(__name__)
app.debug=True
@app.route('/',methods=['GET','POST'])
def wechat_auth():
if request.method == 'GET':
token='your token' #微信配置所需的token
data = request.args
signature = data.get('signature','')
timestamp = data.get('timestamp','')
nonce = data.get('nonce','')
echostr = data.get('echostr','')
s = [timestamp,nonce,token]
s.sort()
s = ''.join(s)
if (hashlib.sha1(s).hexdigest() == signature):
return make_response(echostr)
else:
rec = request.stream.read()
xml_rec = ET.fromstring(rec)
tou = xml_rec.find('ToUserName').text
fromu = xml_rec.find('FromUserName').text
content = xml_rec.find('Content').text
xml_rep = "%s%s%stext%s0"
response = make_response(xml_rep % (fromu,tou,str(int(time.time())), content))
response.content_type='application/xml'
return response
return 'Hello weixin!'
if __name__ == '__main__':
app.run()
注意 其中的
response = make_response(xml_rep % (fromu,tou,str(int(time.time())), content))
# 是将xml_rep 的值按顺序返回给微信
这个是返回值。 所以如果要做什么微信自动回复 就将内容添到content中,然后微信前端就会回复给用户,如果需要继续深入关于微信返回值的问题 可以看这篇 https://www.oschina
部署完获得后台网址就可以进行下一步:开始填写微信接口。 其他的都随意填,关键的两个值是下图的箭头所指。
填好后会出现配置成功的标志。 这时就能在微信上使用这个公众号了。 本文所实现的功能是你向公众号发布一条信息,公众号返回同样的信息给你
。。。。
哥们只能帮你到这了。 有什么细节不懂再问我吧