公衆號快速部署。
首先需要以下資源: 服務器url (這裏用的是新浪SAE ,不會部署的教程 鏈接在此 http://blog.csdn.net/qq_34963461/article/details/52936665)
微信公衆測試號: 去微信公衆平臺申請測試號,注意 測試號不需要審覈。
首先,需要寫 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 = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>" 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.net/code/snippet_1768500_37529
部署完獲得後臺網址就可以進行下一步:開始填寫微信接口。 其他的都隨意填,關鍵的兩個值是下圖的箭頭所指。
填好後會出現配置成功的標誌。 這時就能在微信上使用這個公衆號了。 本文所實現的功能是你向公衆號發佈一條信息,公衆號返回同樣的信息給你