Python脚本--微信公众号自定义菜单的创建及获取

  暑假写的一个修改/获取微信公众号菜单的脚本,具体看微信公众平台的API:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432

# -*- coding: utf-8 -*
import tkMessageBox
import urllib2
import json
import sys
reload(sys)
sys.setdefaultencoding('UTF-8')

appID = 'wxa46e26b3d591f9f2'      //测试号信息
appsecret = '83ee70dbb6dbc1e090971554b1c7981b'   //同上

menu = {                      //菜单信息,包括一级菜单和二级菜单
    "button":[
    {
        "type": "view",       //type还可以是微信小程序等,此例子只用了一种type。具体可以去看API
        "name": "百度",
        "url": baidu,
    },
    {
        "name": "腾讯",
        "sub_button": [
            {
                "type": "view",
                "name": "腾讯主页",
                "url": "http://www.tencent.com"
            },
            {
                "type": "view",
                "name": "腾讯招聘",
                "url" : "https://join.qq.com/"
            },
            {
                "type": "view",
                "name": "腾讯网",
                "url": "https://www.qq.com/"
            },
            {
                "type": "view",
                "name": "腾讯视频",
                "url": "https://v.qq.com/"
            }
        ]
    },
    {
        "name": "博客园",
        "sub_button": [
            {
                "type": "view",
                "name": "我的博客",
                "url": "https://www.cnblogs.com/cy708/"
            },
            {
                "type": "view",
                "name": "博客园主页",
                "url": "https://www.cnblogs.com/"
            }
        ]
    }
    ]
}

def getMenuRequest():
    gettoken = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appID + '&secret=' + appsecret
    f = urllib2.urlopen(gettoken)
    stringjson = f.read()
    access_token = json.loads(stringjson)['access_token']
    posturl = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + access_token

    req = urllib2.Request(posturl)
    data = urllib2.urlopen(req).read()
    data = json.loads(data)
    return data

def createMenuRequest(menu):
    data = json.dumps(menu,ensure_ascii=False).encode('utf-8')
    gettoken = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appID + '&secret=' + appsecret
    f = urllib2.urlopen(gettoken)
    stringjson = f.read()
    access_token = json.loads(stringjson)['access_token']
    postcreateurl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token

    req = urllib2.Request(postcreateurl)
    response = urllib2.urlopen(req, menu)
    result = response.read()
    result = json.loads(result)     //到这步就可以了,下面的只是提示框显示结果而已
    if result["errcode"] == 0:
        tkMessageBox.showinfo('成功!',"errmsg:"+str(result["errmsg"]))
    else:
        tkMessageBox.showinfo('失败!', "errcode:"+str(result["errcode"])+"\n"+"errmsg:" + str(result["errmsg"]))

 

转载于:https://www.cnblogs.com/cy708/p/10049977.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值