微信开发 之 自定义菜单

http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html

  这是微信公众平台上的说明地址。

  另外订阅号是没有这个权限的,认证过的公众号才可以有这个权限。

  

接口调用请求说明

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

{
     "button":[
     {	
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "name":"菜单",
           "sub_button":[
           {	
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"视频",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
 }



从说明文档上我们可以看出,只要我们把菜单改写成这种json格式,然后以Post的方式提交到指定的地址就可以。


  
            FileStream fs1 = new FileStream(Server.MapPath(".") + "\\Btnconfig.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("UTF-8"));
            string menu = sr.ReadToEnd();
            sr.Close();
            fs1.Close();

  首先,获取到文本文件中写的菜单配置文件的内容。

 

 string url = GetPage("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=****&secret=****", "POST", "");
            TokenModel token = Newtonsoft.Json.JsonConvert.DeserializeObject<TokenModel>(url);
            if (token.access_token != null)
                Response.Write(token.access_token);
            else
            {
                ResultModel rm = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultModel>(url);
                Response.Write(rm.errcode);
            }

  获取到你公众号的Token

 

 string message = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token.access_token + "", "POST", menu);
            Response.Write(message);

  Post到指定的地址。看下返回码,是不是0,是的话,就成功了,如果不是,看下下面这个地址,看看是哪里出错了

  http://mp.weixin.qq.com/wiki/17/fa4e1434e57290788bde25603fa2fcbd.html

 public string GetPage(string posturl, string Method, string postData)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = Encoding.UTF8;
            byte[] data = encoding.GetBytes(postData);
            // 准备请求...  
            try
            {
                // 设置参数  
                request = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = Method;
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据  
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求  
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页(html)代码  
                string content = sr.ReadToEnd();
                string err = string.Empty;
                return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return string.Empty;
            }
        }

  

微信开发 之 自定义菜单

上一篇:微信架构演变


下一篇:AndroidRichText 让Textview轻松的支持富文本(图像ImageSpan、点击效果等等类似QQ微信聊天)