公眾號基本配置 token 驗證失敗,成功解決


官網提供代碼適用於python2.7+版本。

當你用python3+版本運行,驗證token肯定失敗。

需要修改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 = "12345678"
            
       # 以下5行是官網提供適用於 python 2.7+ 版本的代碼 #list = [token, timestamp, nonce] #list.sort() #sha1 = hashlib.sha1() #map(sha1.update, list) #hashcode = sha1.hexdigest()
       # 以下7行是python3+能驗證通過的代碼  list
= [token, timestamp, nonce] list.sort() sha1 = hashlib.sha1() sha1.update(list[0].encode('utf-8')) sha1.update(list[1].encode('utf-8')) sha1.update(list[2].encode('utf-8')) hashcode = sha1.hexdigest() print("handle/GET func: hashcode, signature: ", hashcode, signature) if hashcode == signature: return echostr else: return "" except (Exception, Argument): return Argument

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM