- 登录微信公众号小程序后台 找到–>开发–>开发管理–>开发设置
- 配置相关信息, 如图:
- 修改或添加(注意: 必须选择xml 安全模式, 不然可能造成相关的回调结果为空)
- 根据所填写的URl 域名地址 在服务端进行API 接口 编写 验证
示例代码:
class WeiXinToken(APIView):
def get (self, request ):
signature = request.GET.get ( 'signature' )
timestamp = request.GET.get ( 'timestamp' )
nonce = request.GET.get ( 'nonce' )
echostr = request.GET.get ( 'echostr' )
token = "*******填写后台配置的Token令牌********"
tmpArr = [ token, timestamp, nonce ]
tmpArr.sort ()
string = ''.join ( tmpArr ).encode ( 'utf-8' )
string = hashlib.sha1 ( string ).hexdigest ()
if string == signature:
return HttpResponse ( echostr )
else:
return HttpResponse ( "false" )
- 验证成功之后,编写微信消息回调接口逻辑获取相应的回调结果
回调结果需要解密, 解密包下载地址: c++, php, java, python, c# 5
代码示例:
class WeiXinToken(APIView):
"""当前只获取到了xml 信息, 后续更新"""
def post(self,request):
signature = request.GET.get ( 'signature' )
timestamp = request.GET.get ( 'timestamp' )
nonce = request.GET.get ( 'nonce' )
msg_sign = request.GET.get ( 'msg_signature' )
token = "*******填写后台配置的Token令牌********"
encodingAESKey = "*****对应后台配置的消息加密密钥*****"
_xml = request.body
# 拿到微信发送的xml请求 即微信支付后的回调内容
xml = str(_xml, encoding="utf-8")
# 解密模块
decrypt_test = WXBizMsgCrypt(token, encodingAESKey, APP_ID)
ret, decryp_xml = decrypt_test.DecryptMsg(xml, msg_sign, timestamp, nonce)
print(ret, decryp_xml)
return HttpResponse("SUCCESS")