2013-0810

微信公众号接口:40016 invalid button size

作者: momy 分类: 编程开发 3 Comment »
摘要:在做微信自定义菜单开发的时候报40016 invalid button size这个错误

在做微信自定义菜单开发的时候报40016 invalid button size这个错误,莫名奇妙的,2-5个菜单数目也对啊,下边分享下C#版代码:

系列化类:

[Serializable]
public class Button
{
    public string name { get; set; }
}
[Serializable]
public class SubButton : Button
{
    public string type { get; set; }
    public string key { get; set; }
}
[Serializable]
public class Menu:Button
{
    public Button[] sub_button { get; set; }
}
[Serializable]
public class WeixinMenu
{
    public List<Menu> button { get; set; }
}

特别说明,以上变量名必须小写,否则回报40016 invalid button size,好蛋疼的错误,腾讯竟然要区分大小写。

创建菜单方法:

WeixinAccessTokenModel weixinAccessTokenModel = GetWeixinAccessToken();
string url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + weixinAccessTokenModel.access_token;
List<Menu> menus = new List<Menu>();
menus.Add(
    new Menu()
    {
        name = "求职招聘",
        sub_button = new SubButton[]
        {
            new SubButton()
            {
                name = "求职",
                key = "menu_qiuzhi",
                type = "click"
            },
            new SubButton()
            {
                name = "招聘",
                key = "menu_zhaopin",
                type = "click"
            }
        }
    });
menus.Add(
    new Menu()
    {
        name = "供应需求",
        sub_button = new SubButton[]
        {
            new SubButton()
            {
                name = "供应",
                key = "menu_gongying",
                type = "click"
            },
            new SubButton()
            {
                name = "需求",
                key = "menu_xuqiu",
                type = "click"
            }
        }
    });
WeixinMenu weixinMenu = new WeixinMenu() { button = menus };
JavaScriptSerializer jss = new JavaScriptSerializer();
string menusString = jss.Serialize(weixinMenu);
byte[] postBytes=Encoding.UTF8.GetBytes(menusString);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
using (Stream reqStream = request.GetRequestStream())
{
    reqStream.Write(postBytes, 0, postBytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
string responseText = sr.ReadToEnd();
sr.Close();
sr.Dispose();
responseStream.Close();
string jsonData = responseText;
MenuResult menuResult = jss.Deserialize<MenuResult>(jsonData);
return url + "\n" + menuResult.Errcode + "\n" + menuResult.Errmsg;


标签: 微信公众号接口 自定义菜单 40016 invalid button size 阅读: 67872
上一篇: MVC ERROR: The ID field is required.错误原因及解决办法 - 25677次
下一篇: 分享一个HTML5的矩阵页面 - 17183次

网友评论

momy 2014/1/14 19:26:30

注意大小写啊 

吃符号的亏,上大小写的当 一点不为过啊

momy 2014/3/7 16:14:42

报这个错误,多半是json格式错误,多了其他数据也不行,少了也不行,要求较严格

119.145.12.* 2015/7/17 16:32:25

refedfrds

向右滑动解锁留言