微信公众号-微信接口
阅读 (500924)接入微信公众平台开发,开发者需要按照如下步骤完成:
- 填写服务器配置
- 验证服务器地址的有效性
- 依据接口文档实现业务逻辑
填写服务器配置
说明:现在选择提交肯定是验证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") @csrf_exempt 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服务
-
公众平台点击提交
自有公众号开发:
微信测试平台: