共计 3231 个字符,预计需要花费 9 分钟才能阅读完成。
接入微信公众平台开发,开发者需要按照如下步骤完成:
- 填写服务器配置
- 验证服务器地址的有效性
- 依据接口文档实现业务逻辑
填写服务器配置
说明:现在选择提交肯定是验证 token 失败,因为还需要完成代码逻辑
注意:如果没有注册公众号,也可以利用测试平台完成上述过程 ( 在开发过程中建议使用测试账号,待真实上线时使用自己真实的公众号即可)
测试平台:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
验证服务器地址的有效性
开发者提交信息后,微信服务器将发送 GET 请求到填写的服务器地址 URL 上,GET 请求携带四个参数
-
原理
开发者通过检验 signature 对请求进行校验。若确认此次 GET 请求来自微信服务器,请原样返回 echostr 参数内容,则接入生效,成为开发者成功,否则接入失败
-
流程
- 将 token、timestamp、nonce 三个参数进行字典序排序
- 将三个参数字符串拼接成一个字符串进行 sha1 加密
- 开发者获得加密后的字符串可与 signature 对比,标识该请求来源于微信
-
搭建 Django 服务
-
创建 Django 工程并添加应用
-
修改配置文件 settings.py
ALLOWED_HOSTS = ["*"] # Application definition INSTALLED_APPS = ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myApp', ] -
添加主路由与子路由
project/urls.py
from django.contrib import admin from django.urls import path, re_path, include urlpatterns = [path('admin/', admin.site.urls), path('', include(("myApp.urls", "myApp"), namespace="myApp")), ] myApp/urls.py
from django.urls import path, re_path from myApp import views urlpatterns = [path(r'index/', views.index), ] -
添加测试视图
myApp/views.py
from django.shortcuts import render, HttpResponse # Create your views here. def index(request): return HttpResponse("sunck is a good man") -
测试基础服务
启动命令:
python manage.py runserver 0.0.0.0:8080
浏览器地址栏输入:
http://39.107.226.105:8080/index/
-
验证微信服务器请求
myApp/urls.py
from django.urls import path, re_path from myApp import views urlpatterns = [path(r'index/', views.index), path(r'weixin/', views.weixin), ] myApp/views.py
from django.shortcuts import render, HttpResponse from django.views.decorators.csrf import csrf_exempt import hashlib def index(request): return HttpResponse("sunck is a good man") def weixin(request): if request.method == "GET": # 接收微信服务器 get 请求发过来的参数 signature = request.GET.get('signature', None) timestamp = request.GET.get('timestamp', None) nonce = request.GET.get('nonce', None) echostr = request.GET.get('echostr', None) # 服务器配置中的 token token = 'sunck' # 把参数放到 list 中排序后合成一个字符串,再用 sha1 加密得到新的字符串与微信发来的 signature 对比,如果相同就返回 echostr 给服务器,校验通过 hashlist = [token, timestamp, nonce] hashlist.sort() hashstr = ''.join([s for s in hashlist]) hashstr = hashlib.sha1(hashstr.encode("utf-8")).hexdigest() if hashstr == signature: return HttpResponse(echostr) else: return HttpResponse("field") -
启动 Django 服务
启动服务:
python manage.py runserver 0.0.0.0:8080
-
配置 Nginx
因为微信服务器只请求 80 或者 443 端口,但是 DJango 服务无法使用这两个端口。所以需要配合 Nginx 作为反向代理分发请求给 DJango 服务
user nginx; | |
worker_processes 1; | |
error_log /var/log/nginx/error.log; | |
pid /run/nginx.pid; | |
include /usr/share/nginx/modules/*.conf; | |
events { | |
use epoll; | |
worker_connections 1024; | |
} | |
http {log_format main '$remote_addr - $remote_user [$time_local]"$request"' | |
'$status $body_bytes_sent"$http_referer" ' | |
'"$http_user_agent" "$http_x_forwarded_for"'; | |
access_log /var/log/nginx/access.log main; | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
keepalive_timeout 65; | |
types_hash_max_size 2048; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
include /etc/nginx/conf.d/*.conf; | |
server {listen 80 default_server; | |
listen [::]:80 default_server; | |
server_name _; | |
charset koi8-r; | |
location / {proxy_pass http://39.107.226.105:8080; | |
} | |
location /static {alias /var/www/ty/collectedstatic/;} | |
error_page 404 /404.html; | |
location = /40x.html { } | |
error_page 500 502 503 504 /50x.html; | |
location = /50x.html {}} | |
} |
重启 Nginx 服务:systemctl restart nginx.service
浏览器地址栏输入:http://39.107.226.105/index/
注意:此时无需输入 8080 端口,默认使用 80 端口请求 Nginx 服务,Nginx 再将请求转发给 DJango 服务
-
公众平台点击提交
自有公众号开发:
微信测试平台:
