1、在接入微信公众平台之前,需要在微信公众平台配置好基本信息,如下:

python Django框架接入微信公众平台

这个时候点击“提交”按钮,会提示“Token校验失败”,不要着急,这是必然会出现的现象,先不要退出页面,保留各项输入的数据,按第二步操作 

2、编写代码校验微信后台提供的数据
views.py
from django.http.response import HttpResponse
import hashlib
from django.views.decorators.csrf import csrf_exempt


@csrf_exempt
def check_signature(request):
    if request.method == 'GET':
        signature = request.GET.get('signature')
        timestamp = request.GET.get('timestamp')
        nonce = request.GET.get('nonce')
        echostr = request.GET.get('echostr')
        token = 'leartd'

        hashlist = [token, timestamp, nonce]
        hashlist.sort()
        print('[token, timestamp, nonce]', hashlist)
        hashstr = ''.join([s for s in hashlist]).encode('utf-8')  #这里必须增加encode('utf-8'),否则会报错
        print('hashstr befor sha1', hashstr)
        hashstr = hashlib.sha1(hashstr).hexdigest()
        print('hashstr sha1', hashstr)
        if hashstr ==signature:
            return HttpResponse(echostr)  #必须返回echostr
        else:
            return HttpResponse('error')  #可根据实际需要返回
    else:
        return HttpResponse('chenggong')  #可根据实际需要返回

url规则配置不在这里赘述

3、将项目代码提交到服务器后,启动服务。并执行步骤1中的“提交”按钮。此时提示提交成功。

接下来可以进行其他的业务代码开发



相关文章:

  • 2021-12-24
  • 2022-02-15
  • 2022-12-23
  • 2022-01-03
  • 2022-12-23
  • 2021-09-23
  • 2021-12-13
猜你喜欢
  • 2021-07-28
  • 2021-12-06
  • 2021-06-11
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案