url没错 Token没错 后台也有日志输出,直接返回echostr
公网可以访问,python写的后台
handle.py
# -*- coding: utf-8 -*-
# filename: handle.py
import hashlib
import web
class Handle(object):
def GET(self):
try:
data = web.input()
print(data)
if len(data) == 0:
return "hello, this is handle view"
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "shaiden109" #请按照公众平台官网\基本配置中信息填写
list = [token, timestamp, nonce]
list.sort()
sha1 = hashlib.sha1()
map(sha1.update, list)
hashcode = sha1.hexdigest()
print("handle/GET func: hashcode, signature: ", hashcode, signature)
if hashcode == signature:
return echostr
else:
return ""
except Exception as e:
print(e)
return e
曾经做过公众号,都是因为token这个问题没做完,这个服务器已经加入白名单了。
请教下,返回文本的时候要怎么写啊,我这里一直认证失败
问题已经解决!将
sha1 = hashlib.sha1() map(sha1.update, list) hashcode = sha1.hexdigest()
替换成
hashcode = hashlib.sha1("".join(list).encode('utf8')).hexdigest()
代码如下:
if request.method == 'GET':
signature = request.args.get("signature", "")
timestamp = request.args.get("timestamp", "")
nonce = request.args.get("nonce", "")
echostr = request.args.get("echostr", "")
print("原始:",signature, timestamp, nonce, echostr)
token = "wxtokenone" # 这里是你写的那个token值
data = [token, timestamp, nonce]
data.sort() # 3、三个参数拼接成一个字符串并进行sha1加密
temp = ''.join(data)
sha1 = hashlib.sha1(temp.encode('utf-8'))
hashcode = sha1.hexdigest()
print("新的:",hashcode) # 4、对比获取到的signature与根据上面token生成的hashcode,如果一致,则返回echostr,对接成功
if hashcode == signature:
return echostr
else:
return "error"