微信公众号接口: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;
最近评论