说实话,微信公众号给的文档里面的代码不靠谱,我是用django的,浪费了4个小时,经过研究,使用如下代码才得到正确的哈希值:
def wx(request): signature = request.GET['signature'] timestamp = request.GET['timestamp'] nonce = request.GET['nonce'] echostr = request.GET['echostr'] token = "××××××" # 请按照公众平台官网\基本配置中信息填写 list = [token, timestamp, nonce] list.sort() hashstr = ''.join([s for s in list]) sha1 = hashlib.sha1() sha1.update(hashstr.encode('utf8')) hashcode = sha1.hexdigest() # 把我们生成的字符串和微信服务器发送过来的字符串比较, # 如果相同,就把服务器发过来的echostr字符串返回去 if hashcode == signature: return HttpResponse(echostr) else: return HttpResponse('')