django - 微信公共平台第三方接口python实现,无法返回数据给客户端
大家讲道理
大家讲道理 2017-04-17 11:26:12
[Python讨论组]
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
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(4)
伊谢尔伦

我现在自己的微信平台没有通过认证,暂时看不了API是否发生了改变,但是我记得公众平台返回XML内容就可以了,是否是模板的原因影响了结果?

你可以直接把xml返回出来就可以了

大家讲道理

添加一个response header试试: content_type = 'application/xml'

黄舟

我用了一个开源的公众平台的框架 werobot,在github搜索就OK了,可以自己拿来改改用。看看他的代码,你大概就明白了。

巴扎黑

你好,我最近也在看这个平台。初学python,python+django服务器搭建好了.我的问题是,怎么在服务器下运行这个py文件,从而让微信服务器验证。应该怎么配置?谢谢。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号