分类:
微信公众号
http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
ACCESS_TOKEN的获取,请参考《微信公众号Access_token获取(二)》,然而我们注册的公众号是没有这个自定义菜单接口权限的,可以申请接口测试号来获取ACCESS_TOKEN使用。
click和view的请求示例
{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"name":"菜单",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"miniprogram",
"name":"wxa",
"url":"http://mp.weixin.qq.com",
"appid":"wx286b93c14bbf93aa",
"pagepath":"pages/lunar/index"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
} 通过代码实现(于后台控制台实现):
public ActionResult CustomMenu()
{
HttpClient httpClient = new HttpClient();
//得到所需要的参数格式
List<WxMenuDTO> wxMenuDTO_Sub = new List<WxMenuDTO>();
wxMenuDTO_Sub.Add(new WxMenuDTO() { name = "搜索", type = "click", key = "vx_menu_search" });
wxMenuDTO_Sub.Add(new WxMenuDTO() { name = "魂器", type = "click", key = "vx_menu_hq" });
wxMenuDTO_Sub.Add(new WxMenuDTO() { name = "管理", type = "click", key = "vx_menu_gl" });
wxMenuDTO_Sub.Add(new WxMenuDTO() { name = "设置", type = "click", key = "vx_menu_sz" });
List<WxMenuDTO> wxMenuDTOs_Button = new List<WxMenuDTO>();
wxMenuDTOs_Button.Add(new WxMenuDTO() { name = "菜单", sub_button = wxMenuDTO_Sub });
wxMenuDTOs_Button.Add(new WxMenuDTO() { name = "新闻", key = "vx_menu_More", type = "click" });
wxMenuDTOs_Button.Add(new WxMenuDTO() { name = "更多", url = "http://www.tnblog.net/gxl990119/article/details/2995",
type = "view" });
var tempObj = new { button = wxMenuDTOs_Button };
//输出josn格式(序列化)。
string jsonstr = JsonConvert.SerializeObject(tempObj);
StringContent stringContent = new StringContent(jsonstr);
//post请求
string result = httpClient.PostAsync(" https://api.weixin.qq.com/cgi-bin/menu/create?access_token="
+ , stringContent).Result.Content.ReadAsStringAsync().Result;
return Json(result);
}
注:WxMenuDTO类(需要的格式参数属性)
public class WxMenuDTO
{
public string name { get; set; }
public string type { get; set; }
public string url { get; set; }
public string appid { get; set; }
public string pagepath { get; set; }
public string key { get; set; }
public List<WxMenuDTO> sub_button { get; set; }
}正确时的返回JSON数据包如下:
{"errcode":0,"errmsg":"ok"}
然后可以到申请接口测试号的页面扫描二维码关注公众号查看效果,以上代码实现效果:
评价
