1、在接入微信公眾平台之前,需要在微信公眾平台配置好基本信息,如下:
這個時候點擊“提交”按鈕,會提示“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中的“提交”按鈕。此時提示提交成功。
接下來可以進行其他的業務代碼開發