1.再次重复说明,下面代码只是为了简单说明接口获取方式。实际中并不推荐,尤其是业务繁重的公众号,更需要中控服务器,统一的获取accessToken。

vim basic.py
#name: basic.py
import urllib
import time
import json
class Basic:
         def __init__(self):
              self.__accessToken = \'\'
              self.__leftTime = 0
         def __real_get_access_token(self):
              appId = ""
              appSecret = ""
              postUrl = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % (appId, appSecret))
              urlResp = urllib.urlopen(postUrl)
              urlResp = json.loads(urlResp.read())
              print urlResp
              self.__accessToken = urlResp[\'access_token\']
              self.__leftTime = urlResp[\'expires_in\']
         def get_access_token(self):
              if self.__leftTime < 10:
                    self.__real_get_access_token()
                    return self.__accessToken
         def run(self):
              while(True):
                  if self.__leftTime > 10:
                       time.sleep(2)
                       self.__leftTime -= 2
                  else:
                       self.__real_get_access_token()

2.自定义菜单
自定义菜单意义作用请参考官方wiki 介绍。

目标:三个菜单栏,体验click、view、media_id 三种类型的菜单按钮,其他类型在本小节学习之后,自行请查询公众平台wiki说明领悟。

2.1 创建菜单界面
1)根据公众平台wiki 给的json 数据编写代码,其中涉及media_id部分请阅读”永久素材”章节。vim menu.py

# -*- coding: utf-8 -*-
# filename: menu.py
import urllib
from basic import Basic

class Menu(object):
    def __init__(self):
        pass
    def create(self, postData, accessToken):
        postUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s" % accessToken
        if isinstance(postData, unicode):
            postData = postData.encode(\'utf-8\')
        urlResp = urllib.urlopen(url=postUrl, data=postData)
        print urlResp.read()

    def query(self, accessToken):
        postUrl = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=%s" % accessToken
        urlResp = urllib.urlopen(url=postUrl)
        print urlResp.read()

    def delete(self, accessToken):
        postUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=%s" % accessToken
        urlResp = urllib.urlopen(url=postUrl)
        print urlResp.read()

    #获取自定义菜单配置接口
    def get_current_selfmenu_info(self, accessToken):
        postUrl = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=%s" % accessToken
        urlResp = urllib.urlopen(url=postUrl)
        print urlResp.read()

if __name__ == \'__main__\':
    myMenu = Menu()
    postJson = """
    {
        "button":
        [
            {
                "type": "click",
                "name": "开发指引",
                "key":  "mpGuide"
            },
            {
                "name": "公众平台",
                "sub_button":
                [
                    {
                        "type": "view",
                        "name": "更新公告",
                        "url": "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1418702138&token=&lang=zh_CN"
                    },
                    {
                        "type": "view",
                        "name": "接口权限说明",
                        "url": "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1418702138&token=&lang=zh_CN"
                    },
                    {
                        "type": "view",
                        "name": "返回码说明",
                        "url": "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433747234&token=&lang=zh_CN"
                    }
                ]
            },
            {
                "type": "media_id",
                "name": "旅行",
                "media_id": "z2zOokJvlzCXXNhSjF46gdx6rSghwX2xOD5GUV9nbX4"
            }
          ]
    }
    """
    accessToken = Basic().get_access_token()
    #myMenu.delete(accessToken)
    myMenu.create(postJson, accessToken)

2)在腾讯云服务器上执行命令:python menu.py。

版权声明:本文为yu-hailong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yu-hailong/p/8312833.html