+发表新主题
查看: 1347|回复: 0

[代码片段] 使用python创建查询删除微信公众平台的自定义菜单

[复制链接]

[代码片段] 使用python创建查询删除微信公众平台的自定义菜单

[复制链接]
MONDEO 发表于 2013-12-26 17:16:14 浏览:  1347 回复:  0 [显示全部楼层] 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Created on 2013年10月28日

  4. @author: 坏坏的忧伤
  5. '''
  6. import urllib.request
  7. import json


  8. class MenuManager:
  9.     accessUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret"
  10.     delMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="
  11.     createUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="
  12.     getMenuUri="https://api.weixin.qq.com/cgi-bin/menu/get?access_token="
  13.     def getAccessToken(self):
  14.         f = urllib.request.urlopen(self.accessUrl)
  15.         accessT = f.read().decode("utf-8")
  16.         jsonT = json.loads(accessT)
  17.         return jsonT["access_token"]
  18.     def delMenu(self, accessToken):
  19.         html = urllib.request.urlopen(self.delMenuUrl + accessToken)
  20.         result = json.loads(html.read().decode("utf-8"))
  21.         return result["errcode"]
  22.     def createMenu(self, accessToken):
  23.         menu = '''{
  24.                  "button":[
  25.                      {   
  26.           "type":"click",
  27.           "name":"今日歌曲",
  28.           "key":"V1001_TODAY_MUSIC"
  29.       },
  30.       {
  31.            "type":"view",
  32.            "name":"歌手简介",
  33.            "url":"http://www.qq.com/"
  34.       },
  35.       {
  36.            "name":"菜单",
  37.            "sub_button":[
  38.             {"type":"click","name":"hello word","key":"V1001_HELLO_WORLD"},{"type":"click","name":"赞一下我们","key":"V1001_GOOD"}]}]}'''
  39.         html = urllib.request.urlopen(self.createUrl + accessToken, menu.encode("utf-8"))
  40.         result = json.loads(html.read().decode("utf-8"))
  41.         return result["errcode"]
  42.     def getMenu(self):
  43.         html = urllib.request.urlopen(self.getMenuUri + accessToken)
  44.         print(html.read().decode("utf-8"))
  45.      

  46. if __name__ == "__main__":
  47.     wx = MenuManager()
  48.     accessToken = wx.getAccessToken()
  49.     #print(wx.delMenu(accessToken))   #删除菜单
  50.     #print(wx.createMenu(accessToken))  #创建菜单
  51.     wx.getMenu()
复制代码

回复

使用道具 举报


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版| 赣南网

© 2013-2016 Comsenz Inc. Powered by Discuz! X3.4

用微信扫一扫

赣南网