您的位置:首页 > 移动开发 > 微信开发

微信公众号开发创建菜单栏(二)

2015-11-12 15:43 525 查看
创建菜单栏





需要用到http的get和post:http://www.cnblogs.com/lswbk/p/4940449.html

官方自定义菜单创建接口API:http://mp.weixin.qq.com/wiki/2/88b2bf1265a707c031e51f26ca5e6512.html

接口调用请求说明

第一步获取access_token,获取access_token官方API:http://mp.weixin.qq.com/wiki/2/88b2bf1265a707c031e51f26ca5e6512.html

http请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

APPID, APPSECRET在微信公众平台可以找到

string access_token_json = HttpGet("https://api.weixin.qq.com/cgi-bin/token", "grant_type=client_credential&appid=" +APPID + "&secret=" + APPSECRET);
string access_token = string.Empty;
DataTable dt1 = JsonConvert.DeserializeObject<DataTable>("[" + access_token_json + "]");
for (int i = 0; i < dt1.Rows.Count; i++)
{
DataRow dr = dt1.Rows[i];
access_token = dr[0].ToString();
}


第二步设置自定义菜单

http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

参数有access_token,和一个json格式的数据(可以参考官方api,这里只列出click方式,点击菜单事件)

string content="{\"button\": [{\"name\": \"账号服务\", \"sub_button\": [{\"type\": \"click\", \"name\": \"查询用户名\", \"key\": \"Select_username\"}, {\"type\":\"click\", \"name\": \"找回密码\", \"key\": \"Retrieve_password\"}]}]}"

string json = HttpPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token,content);

string errmsg= string.Empty;
DataTable dt2 = JsonConvert.DeserializeObject<DataTable>("[" + json + "]");

for (int i = 0; i < dt2.Rows.Count; i++)
{
DataRow dr = dt2.Rows[i];

errmsg= dr[1].ToString();
}
//if(errmsg=="ok")添加成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: