zoukankan      html  css  js  c++  java
  • 微信开发 之 自定义菜单

    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;
                }
            }
    

      

    喜欢的朋友请帮忙点个赞!!!
  • 相关阅读:
    学而201205期先导班信息系统项目管理师招生简章
    201205期蘑菇班信息系统项目管理师招生简章
    用常识驳倒方舟子之一 :韩寒的女儿与常识
    学而201211期蘑菇班信息系统项目管理师招生简章
    工作量与工作历时计算
    git设置中英文
    楼层跳转scrollintoview坑
    一则浏览器兼容性解决案例(搜狗)
    C语言未初始化变量输出的是随即数
    远程关机,在MM面前显摆一次的机会,别错过 To Phper
  • 原文地址:https://www.cnblogs.com/mchuang/p/5156933.html
Copyright © 2011-2022 走看看