微信公众号,想要接python/django的服务器,卡在了第一步,验证token上,按照文档,获取signature 、timestamp 、nonce 后,验证是否一致,一致的话返回echostr,
def test(request):
signature = request.GET.get("signature","")
timestamp = request.GET.get("timestamp","")
nonce = request.GET.get("nonce","")
echostr=request.GET.get("echostr","")
result={}
result['signature']=signature
result['timestamp']=timestamp
result['nonce']=nonce
result['echostr']=echostr
tmpArr = [TOKEN, timestamp, nonce]
tmpArr.sort()
tmpStr = "".join(tmpArr)
tmpStr = hashlib.sha1(tmpStr.encode()).hexdigest()
if tmpStr == signature:
return echostr
else:
return False
测试结果反馈
AttributeError: 'bool' object has no attribute 'get'
后来一步一步排查,发现,只要把最后return 改成return httpresponse或者return jsonresponse,他就可以正常反馈结果(用本地服务在浏览器直接输入请求连接做测试),但这样一来,在微信公众号的认证token中又没法认证,因为返回的是httpresponse/jsonresponse
怎么解决?
if tmpStr == signature:
return httpresponse(echostr)
else:
return httpresponse(echostr)