阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

微信公众号-微信接口

135次阅读
没有评论

共计 3231 个字符,预计需要花费 9 分钟才能阅读完成。

接入微信公众平台开发,开发者需要按照如下步骤完成:

  1. 填写服务器配置
  2. 验证服务器地址的有效性
  3. 依据接口文档实现业务逻辑

填写服务器配置

微信公众号 - 微信接口

微信公众号 - 微信接口

微信公众号 - 微信接口

说明:现在选择提交肯定是验证 token 失败,因为还需要完成代码逻辑

注意:如果没有注册公众号,也可以利用测试平台完成上述过程 ( 在开发过程中建议使用测试账号,待真实上线时使用自己真实的公众号即可)

测试平台:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

微信公众号 - 微信接口

验证服务器地址的有效性

开发者提交信息后,微信服务器将发送 GET 请求到填写的服务器地址 URL 上,GET 请求携带四个参数

微信公众号 - 微信接口

  • 原理

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

  • 流程

    1. 将 token、timestamp、nonce 三个参数进行字典序排序
    2. 将三个参数字符串拼接成一个字符串进行 sha1 加密
    3. 开发者获得加密后的字符串可与 signature 对比,标识该请求来源于微信
  • 搭建 Django 服务

  1. 创建 Django 工程并添加应用

    微信公众号 - 微信接口

  2. 修改配置文件 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', ]
  3. 添加主路由与子路由

    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), ]
  4. 添加测试视图

    myApp/views.py

    from django.shortcuts import render, HttpResponse # Create your views here. def index(request): return HttpResponse("sunck is a good man")
  5. 测试基础服务

    启动命令:python manage.py runserver 0.0.0.0:8080

    浏览器地址栏输入:http://39.107.226.105:8080/index/

    微信公众号 - 微信接口

  6. 验证微信服务器请求

    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")
  7. 启动 Django 服务

    启动服务:python manage.py runserver 0.0.0.0:8080

  8. 配置 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 服务

微信公众号 - 微信接口

  • 公众平台点击提交

    自有公众号开发:

微信公众号 - 微信接口

微信公众号 - 微信接口

微信测试平台:

微信公众号 - 微信接口

微信公众号 - 微信接口

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-05-26发表,共计3231字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中