首页 技术 正文
技术 2022年11月16日
0 收藏 723 点赞 2,416 浏览 1318 个字

微信系列之公众号Token验证


python3安装web.py可以选择安装“pip install web.py==0.40.dev0`

pycharm连接线上服务器开发

1.打开pycharm > Tools > Deployment

1.添加服务

2.选择SFTP

3.配置信息

1.远程主机地址和商品

2.根主机地址

3.配置用户名和密码,可以选择ssh文件

4.**项目配置文件setting里设置以连接远程解释器


token验证

官方文档中map(sha1.update, list)是无法对sha1进行持续更新哈希值,实验过后其值仍是空字符串的哈希的值,且sha1.update方法需要TypeError: Unicode-objects must be encoded before hashing

微信signaturenonceechostr参数如下:

参数 描述
signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串

验证方法

1.服务器端获取token,nonce,timestamp组成列表

2.列表排序

3.排序后的元素进行摘要

4.摘要比对signature

5.响应echostr

# coding: utf-8
# filename: handle.py
import web
import hashlibclass Handle(object):
def GET(self):
"""
signature 微信加密签名,signature 结合了开发者的
token和请求的 timestamp 与nonce
token 时间戳
nonce 随机数
echostr 随机字符串
:return:
"""
try:
# 请求无参数,即非 token 验证
data = web.input()
if len(data) == 0:
return "Hello, This is handle views"
signature = data.signature
nonce = data.nonce
timestamp = data.timestamp
echostr = data.echostr
token = "******" # 基本配置的 token 填写一样的值
# 对 token timestamp nonce 进行排序后进行摘要
sha1_list = [token, timestamp, nonce]
sha1_list.sort()
sha1 = hashlib.sha1()
list(map(lambda s: sha1.update(s.encode('utf-8')), sha1_list))
hashcode = sha1.hexdigest()
print('func: hashcode, signature: {} {}'.format(hashcode, signature))
if hashcode == signature:
return echostr
else:
return ""
except Exception as e:
return e.reason

参考资料

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297