注意:在微信公眾號的開發者基本配置之后,用戶發送的消息將自動轉發到該設置地址,並且在網站中設置的自動回復和自定義的菜單都將失效!
1) 公眾平台官網登錄之后,找到“基本配置”菜單欄
2) 填寫配置
url填寫:http://外網IP:端口號/wx 。外網IP請到騰訊雲購買成功處查詢, http的端口號固定使用80,不可填寫其他。
Token:自主設置,這個token與公眾平台wiki中常提的access_token不是一回事。這個token只用於驗證開發者服務器。
3) 現在選擇提交肯定是驗證token失敗,因為還需要完成代碼邏輯。改動原先main.py文件,新增handle.py
a)vim main.py
# -*- coding: utf-8 -*- # filename: main.py import web from handle import Handle urls = ( '/wx', 'Handle', ) if __name__ == '__main__': app = web.application(urls, globals()) app.run()
b)vim handle.py
先附加邏輯流程圖
# -*- 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) hashcode = sha1.hexdigest() print "handle/GET func: hashcode, signature: ", hashcode, signature if hashcode == signature: return echostr else: return "" except Exception, Argument: return Argument
4) 重新啟動成功后(python main.py 80),點擊提交按鈕。若提示”token驗證失敗”, 請認真檢查代碼或網絡鏈接等。若token驗證成功,會自動返回基本配置的主頁面,點擊啟動按鈕