公众号开发

公众号开发

微信公众号token验证失败的原因

网络李魔佛 发表了文章 • 0 个评论 • 2502 次浏览 • 2021-12-24 23:42 • 来自相关话题

主要是文档写的过于模糊(比较含蓄,实际上是垃圾到不行)
 
因为提供的文档,用的是python2的代码:
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 = "xxxx" #请按照公众平台官网\基本配置中信息填写

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, Argument:
return Argument
如果按照上面的代码,放到python3上执行, 实际返回的 echostr 的值是错的. 所以服务器拿这个错的值,匹配, 会总是失败.
 
什么年代了, 一个demo 也不换个新一点的python版本.
 
提供一个新版的python3通过检验的代码:
 
 
# -*- coding: utf-8 -*-

# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import web

class Handle(object):

def POST(self):
pass

# get方法,验证token
def GET(self):
try:
data = web.input()
if len(data) == 0:
return "token success!"
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "123456abcdefg" # 请按照公众平台官网\基本配置中信息填写,两个token保持一致

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 查看全部
主要是文档写的过于模糊(比较含蓄,实际上是垃圾到不行)
 
因为提供的文档,用的是python2的代码:
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 = "xxxx" #请按照公众平台官网\基本配置中信息填写

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, Argument:
return Argument

如果按照上面的代码,放到python3上执行, 实际返回的 echostr 的值是错的. 所以服务器拿这个错的值,匹配, 会总是失败.
 
什么年代了, 一个demo 也不换个新一点的python版本.
 
提供一个新版的python3通过检验的代码:
 
 
# -*- coding: utf-8 -*-

# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import web

class Handle(object):

def POST(self):
pass

# get方法,验证token
def GET(self):
try:
data = web.input()
if len(data) == 0:
return "token success!"
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "123456abcdefg" # 请按照公众平台官网\基本配置中信息填写,两个token保持一致

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

微信公众号token验证失败的原因

网络李魔佛 发表了文章 • 0 个评论 • 2502 次浏览 • 2021-12-24 23:42 • 来自相关话题

主要是文档写的过于模糊(比较含蓄,实际上是垃圾到不行)
 
因为提供的文档,用的是python2的代码:
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 = "xxxx" #请按照公众平台官网\基本配置中信息填写

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, Argument:
return Argument
如果按照上面的代码,放到python3上执行, 实际返回的 echostr 的值是错的. 所以服务器拿这个错的值,匹配, 会总是失败.
 
什么年代了, 一个demo 也不换个新一点的python版本.
 
提供一个新版的python3通过检验的代码:
 
 
# -*- coding: utf-8 -*-

# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import web

class Handle(object):

def POST(self):
pass

# get方法,验证token
def GET(self):
try:
data = web.input()
if len(data) == 0:
return "token success!"
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "123456abcdefg" # 请按照公众平台官网\基本配置中信息填写,两个token保持一致

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 查看全部
主要是文档写的过于模糊(比较含蓄,实际上是垃圾到不行)
 
因为提供的文档,用的是python2的代码:
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 = "xxxx" #请按照公众平台官网\基本配置中信息填写

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, Argument:
return Argument

如果按照上面的代码,放到python3上执行, 实际返回的 echostr 的值是错的. 所以服务器拿这个错的值,匹配, 会总是失败.
 
什么年代了, 一个demo 也不换个新一点的python版本.
 
提供一个新版的python3通过检验的代码:
 
 
# -*- coding: utf-8 -*-

# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import web

class Handle(object):

def POST(self):
pass

# get方法,验证token
def GET(self):
try:
data = web.input()
if len(data) == 0:
return "token success!"
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "123456abcdefg" # 请按照公众平台官网\基本配置中信息填写,两个token保持一致

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