def weixin(request): try: # 微信接口认证 使用GET方式 if request.method == 'GET': token = 'air2you' tmpArr =[token, request.GET['timestamp'], request.GET['nonce']] tmpArr.sort() tmpArr.sort() tmpStr = ''.join(tmpArr) code = hashlib.sha1(tmpStr).hexdigest() if code == request.GET['signature']: return render_to_response('air/weixin.html',{'echostr':request.GET['echostr']}) else: return render_to_response('air/weixin.html',{'echostr':''}) # 微信接口通讯 返回用户需要数据 elif request.method == 'POST': xml = et.fromstring(request.raw_post_data) _to = xml.find('FromUserName').text _from = xml.find('ToUserName').text _content = 'welcome!' _type = 'text' return render_to_response('air/weixin.xml',{'_to':_to, '_from': _from, '_time' : int(time.time()), '_type': _type, '_content' : _content}, mimetype='application/xml') except Exception, e: return render_to_response('air/weixin.html',{'echostr':''}) #weixin.xml <xml> <ToUserName><![CDATA[{{ _to }}]]></ToUserName> <FromUserName><![CDATA[{{ _from }}]]></FromUserName> <CreateTime>{{ _time }}</CreateTime> <MsgType><![CDATA[{{ _type }}]]></MsgType> <Content><![CDATA[{{ _content }}]]></Content> <FuncFlag>0</FuncFlag> </xml>
先给你一个我自己实现的示例,你先看看,微信公众平台只能在生产环境调试,这点很不好。如果还不行,可以联系我,微博:唐僧之妈
我现在自己的微信平台没有通过认证,暂时看不了API是否发生了改变,但是我记得公众平台返回XML内容就可以了,是否是模板的原因影响了结果?
你可以直接把xml返回出来就可以了
添加一个response header试试: content_type = 'application/xml'
我用了一个开源的公众平台的框架 werobot,在github搜索就OK了,可以自己拿来改改用。看看他的代码,你大概就明白了。
你好,我最近也在看这个平台。初学python,python+django服务器搭建好了.我的问题是,怎么在服务器下运行这个py文件,从而让微信服务器验证。应该怎么配置?谢谢。