1

微信公众号开发

 3 years ago
source link: https://3wapp.github.io/Python/wechat.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1. wechat-sdk

1.1. 开发者认证配置

接入指南, 成为开发者

开发者通过检验signature对请求进行校验。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。

加密/校验流程如下:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
from flask import Flask
from flask import request

from wechat_sdk import WechatConf
from wechat_sdk import WechatBasic

conf = WechatConf(
    token='',
    appid='',
    appsecret='',
    encrypt_mode='safe',
    encoding_aes_key='',
)

wechat = WechatBasic(conf=conf)

app = Flask(__name__)

@app.route('/weixin', methods=['GET'])
def check():
    args = request.args
    signature = args.get('signature')
    timestamp = args.get('timestamp')
    nonce = args.get('nonce')
    echostr = args.get('echostr')
    if all([signature, timestamp, nonce, echostr]) and wechat.check_signature(signature, timestamp, nonce):
        return echostr
    else:
        return 'error'

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

2. 微信请求

2.1. 格式

POST /weixin?signature=92db6dae514eaec009a55926df68734815d450e9&timestamp=1461049146&nonce=12030128&encrypt_type=aes&msg_signature=20b4e1f1a0e59d4cb670bc082ad2256157cad3d3

<xml><ToUserName><![CDATA[gh_e712ef998048]]></ToUserName>
<FromUserName><![CDATA[ojNiswfpvDLPxYinSkmqI3T0vDdA]]></FromUserName>
<CreateTime>1461049145</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[hi]]></Content>
<MsgId>6275158296027149334</MsgId>
</xml>

2.2. 解析请求

wechat sdk 消息管理接口

err_msg = 'error'
args = request.args
if request.method == 'POST':
    data = request.data
    print args
    print data
    if not parse_request(args, data):
        return err_msg

def parse_request(args, data):
    msg_signature = args.get('msg_signature')
    timestamp = args['timestamp']
    nonce = args['nonce']
    try:
        wechat.parse_data(data, msg_signature, timestamp, nonce)
    except Parse_Error:
        print 'Parse Error'
        return False
    return True

2.3. 被动回复消息 -- 文本消息

将文字信息组装为符合微信服务器要求的响应数据

调用方法:.response_text(content, escape=False)

参数说明:

content: 回复文字
escape: 是否转义该文本内容 (默认不转义)

Recommend

  • 152

    微信公众号批量爬取java版 - ljw不想加班 - 博客园 ljw不想加班

  • 106

    8点1氪 | 微信公众平台全面开放原创功能;广州公交车可刷支付宝乘车;福特将在天猫销售汽车张淇·2017-12-06 23:32每天十亿以上的照片上传,让微信大数据掌...

  • 120

    最近沉迷吃鸡不能自拔,好久没更新文章了。后续将陆续完善《Koa2微信公众号开发》。 一、简介 关于微信公众号的介绍就省略了,自行搜索。注册过程也不说了。我们会直接注册测试号来实现代码。这将会是个全面讲解微信公众号开发的系列教程。本篇是该系列的第一篇,...

  • 35
    • tomoya92.github.io 5 years ago
    • Cache

    微信公众号开发入门

    公司要做微信公众号,又折腾了一遍,总结一下,备忘用 申请测试帐号 个人申请的公众号没有认证的话,是没有那么多权限的,不过微信给我们提供了一个申请测试帐号的页面,申请的测试帐号基本上有所有的权限,申请地址...

  • 32
    • studygolang.com 4 years ago
    • Cache

    Go 语言之微信公众号开发

    前言 前两天分享了一下 网抑云之微信公众号开发 , 现已整理开源,还有许多要完善的地方 <!--more--> 微信公众号开发 ...

  • 4

    记录在使用 uni-app 开发公众号应用时防止被缓存的方法 # 背景 修改页面后重新打包,测试人员在确认问题时总是说没改,页面没有变化,需要进行繁琐的清...

  • 19

    使用NodeJs和JavaScript开发微信公众号 - 前端纸飞机的个人博客 - OSCHINA - 中文开源技术交流社区 大家作为前端可能活多或少的都写过结合微信jsdk开发的微信h5网页,对授权登录这块以及部分sdk可能都有通过后台接口来调用,那么大家有...

  • 7

    [TOC] Hello 各位小伙伴,松哥今天要和大家聊一个有意思的话题,就是使用 Spring Boot 开发微信公众号后台。 很多小伙伴可能注意到松哥的微信公众号后台有一个回复关键字如 666 或者 888 可以获取学习资料的功能,这是松哥...

  • 6

    尝试开发微信公众号消息推送功能并且和小程序关联首页 - Web Design/2019-11-10

  • 6
    • www.cnblogs.com 1 year ago
    • Cache

    微信公众号开发接入 - 天葬

    微信公众号开发 你要有一个微信公众号,一个内网穿透工具 服务器配置:设置与开发-->基本配置-->服务器配置 token:3-32字符,自己生成配置到服务器配置 公网 IP:云服务器一般都有公网IP

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK