Python實現微信公眾平台自訂菜單一實例

來源:互聯網
上載者:User
首先先擷取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.bitsCN.com/photos"      },  {        "type":"view",        "name":"回顧",        "url":"http://m.qzone.com/infocenter?g_f=#2378686916/mine"      },  {        "type":"view",        "name":"美圖app",        "url":"http://bitsCN.com/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.bitsCN.com/projects"      }] }, {      "type":"view",      "name":"設計申請",      "url":"http://bitsCN.com/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)
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.