微信公衆號 ——開發者基本配置(Python3)

https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Getting_Started_Guide.html

這是微信開啓公衆號開發的新手指引,不過他使用的是python2,有些地方不對,尤其是哈希加密那部分,導致驗證一直不通過,參考了https://blog.csdn.net/qq_39866513/article/details/83218731這位博主,終於改好通過了!!!

以下是代碼部分,首先是main.py

import web
from handle import Handle

urls = (
	'/wx' , 'Handle',
	)



if __name__ == '__main__':
	app = web.application(urls,globals())
	app.run()

接着是處理部分

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 = "balaba" #
            print(signature,token,timestamp,nonce,echostr)

            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) as Argument:
            return Argument

 

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