第一步:创建微信公众号
访问微信公众平台官网(https://mp.weixin.qq.com/),使用邮箱注册并创建一
个新的微信公众号。
第二步:开通微信公众号开发者模式
登录微信公众号后台,点击左侧菜单栏的“开发”选项,然后选择“基本配置”。点
击“开启”按钮,开通开发者模式。记录下“AppID”和“AppSecret”。
第三步:创建服务器并搭建 API
本教程使用 Python 和 Flask 搭建 API。请确保您已安装 Python 和 Flask。
1.安装 Flask:
pip install Flask
2.创建一个名为 app.py 的文件,编写如下代码:
from flask import Flask, request, make_response
import hashlibapp = Flask(__name__)@app.route('/weixin',
methods=['GET', 'POST'])
def wechat():if request.method == 'GET':token = 'your_token' # 这
里填写你在微信公众平台设置的 Tokensignature =
request.args.get('signature')timestamp =
request.args.get('timestamp')nonce = request.args.get('nonce')echostr
= request.args.get('echostr')# 验证服务器地址的有效性 if
check_signature(token, signature, timestamp, nonce):return
make_response(echostr)elif request.method == 'POST':# 处理用户发送
的消息 def check_signature(token, signature, timestamp,
nonce):tmp_list = [token, timestamp, nonce]tmp_list.sort()tmp_str =