django后台实现微信公众平台网址接入

django1.3.1 + python2.6.6

前期准备不多说,自己的服务器,自己去申请微信公众平台帐号

在高级功能 --> 开发模式 

填写django配置中你需要的接入的url和token。这块主要是实现接入微信api的时候需要认证的功能

 1 from django.http import HttpResponse
 2 import hashlib
 3 
 4 def checkSignature(request):
 5     '''
 6       验证微信api提供的signature和token等信息  
 7     '''
 8     
 9     token = '自己随意写'
10     signature = request.GET.get('signature', '')
11     timestamp = request.GET.get('timestamp', '')
12     nonce = request.GET.get('nonce',  '')
13     echostr = request.GET.get('echostr', '')
14     
15     infostr = ''.join(sorted([token, timestamp, nonce]))
16     if infostr:
17         hashstr = hashlib.sha1(infostr).hexdigest()
18         if hashstr is signature:
19             return HttpResponse(echostr)
20         else:
21             print 'haststr is not signature'
22     else:
23         print 'infostr does not existing'
24 
25         

views.py中就是这样验证的。通过填写url。然后把写好的url放到微信的接入中去

ok,就会提示成功了。

 

ps:啰嗦一句,最开始google的时候,按照网上的例子写了一下,老是无法验证token。然后就细看了下。微信的验证的页面,就是一个ajax。

通过ajax访问你填写入的那个链接。当然还要加一部分参数。要把echostr返回给ajax,然后微信的后台会去比对。这样才能决定是否验证成功。

有人居然直接return echostr。这样ajax怎么可能接收到你给它返回的echostr呢?

posted on 2013-05-22 16:56  h3idan  阅读(1912)  评论(0编辑  收藏  举报

导航