Python实现微信公众平台自定义菜单实例

yipeiwu_com5年前Python基础

首先先获取access_token,并保存与全局之中

def token(requset):
  url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (
  Config.AppID, Config.AppSecret)
  result = urllib2.urlopen(url).read()
  Config.access_token = json.loads(result).get('access_token')
  print 'access_token===%s' % Config.access_token
  return HttpResponse(result)

利用上面获得的access_token,创建自定义表单

def createMenu(request):
  url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s" % Config.access_token
  data = {
   "button":[
   {
      "name":"看美图",
      "sub_button":[
      {
        "type":"click",
        "name":"美图",
        "key":"meitu"
      },
      {
        "type":"view",
        "name":"精选",
        "url":"http://m.jb51.net/photos"
      },
  {
        "type":"view",
        "name":"回顾",
        "url":"http://m.qzone.com/infocenter?g_f=#2378686916/mine"
      },
  {
        "type":"view",
        "name":"美图app",
        "url":"http://jb51.net/app/app.html"
      }]
 },
 {
      "name":"看案例",
      "sub_button":[
      {
        "type":"click",
        "name":"全部风格",
        "key":"style"
      },
      {
        "type":"click",
        "name":"全部户型",
        "key":"houseType"
      },
  {
        "type":"click",
        "name":"全部面积",
        "key":"area"
      },
  {
        "type":"view",
        "name":"更多案例",
        "url":"http://m.jb51.net/projects"
      }]
 },
 {
      "type":"view",
      "name":"设计申请",
      "url":"http://jb51.net/zhuanti/freedesign.jsp?src=3"

 }

 ]
}
  #data = json.loads(data)
  #data = urllib.urlencode(data)
  req = urllib2.Request(url)
  req.add_header('Content-Type', 'application/json')
  req.add_header('encoding', 'utf-8')
  response = urllib2.urlopen(req, json.dumps(data,ensure_ascii=False))
  result = response.read()
  return HttpResponse(result)

相关文章

解决Python使用列表副本的问题

要使用一个列表的副本,要用切片进行列表复制,这样会形成两个独立的列表。 切记不要将列表赋值给一个列表,因为这样并不能得到两个列表。 1、使用赋值语法创建列表副本的问题 下边就将列表赋值,...

python类的实例化问题解决

python类的实例化问题解决

类的实例化问题解决 运行结果: line 21, in <module> s=speaker('ken',10,'aaa') TypeError: __init__(...

python创建临时文件夹的方法

本文实例讲述了python创建临时文件夹的方法。分享给大家供大家参考。具体实现方法如下: import tempfile, os tempfd, tempname = tempfi...

softmax及python实现过程解析

softmax及python实现过程解析

相对于自适应神经网络、感知器,softmax巧妙低使用简单的方法来实现多分类问题。 功能上,完成从N维向量到M维向量的映射 输出的结果范围是[0, 1],对于一个sample的...

python中文分词教程之前向最大正向匹配算法详解

前言 大家都知道,英文的分词由于单词间是以空格进行分隔的,所以分词要相对的容易些,而中文就不同了,中文中一个句子的分隔就是以字为单位的了,而所谓的正向最大匹配和逆向最大匹配便是一种分词匹...