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