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
{{ _time }}
0
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我现在自己的微信平台没有通过认证,暂时看不了API是否发生了改变,但是我记得公众平台返回XML内容就可以了,是否是模板的原因影响了结果?
你可以直接把xml返回出来就可以了
添加一个response header试试: content_type = 'application/xml'
我用了一个开源的公众平台的框架 werobot,在github搜索就OK了,可以自己拿来改改用。看看他的代码,你大概就明白了。
你好,我最近也在看这个平台。初学python,python+django服务器搭建好了.我的问题是,怎么在服务器下运行这个py文件,从而让微信服务器验证。应该怎么配置?谢谢。