flask測試微信公衆號的配置與連接

環境:騰訊雲+flask+微信公衆號

微信公衆號端配置

使用騰訊雲提供的公網地址:

URL     http://公網IP/wechat8000
Token   toohoo2019

使用flask編寫配置信息測試代碼:

#!/usr/bin/env python
# -*-encoding:UTF-8-*-

from flask import Flask, request, abort
import hashlib

# wechat token
WECHAT_TOKEN = "toohoo2019"

app = Flask(__name__)


@app.route("/wechat8000",methods=['GET','POST'])
def wechat():
    # recive the args
    signature = request.args.get("signature")
    timestamp = request.args.get("timestamp")
    nonce = request.args.get("nonce")
    echostr = request.args.get("echostr")

    # check the args
    if not all([signature, timestamp, nonce, echostr]):
        abort(400)

    # compute signature through the wechat process
    li = [WECHAT_TOKEN, timestamp, nonce]
    # sort
    li.sort()

    tmp_str = "".join(li).encode('utf-8')
    # sha1 encode, and get the correct sign
    sign = hashlib.sha1(tmp_str).hexdigest()

    # check if equal
    if signature != sign:
        # it is not from wechat
        abort(403)
    else:
        return echostr

if __name__ == '__main__':
    # visit private net from public net 
    app.run(host="內網IP",port=80, debug=True)

最後點擊頁面的配置頁面的提交按鈕即可,彈出配置成功即表示微信認可了後端主機,配置頁面連接爲:
https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章