.NET微信公眾號開發-2.0創建自定義菜單


一.前言

開發之前,我們需要閱讀官方的接口說明文檔,不得不吐槽一下,微信的這個官方文檔真的很爛,但是,為了開發我們需要的功能,我們也不得不去看這些文檔.

接口文檔地址:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html

看了這些個文檔,基本意思明白了,就是我們把我們要創建的菜單創建好,post到微信的服務器上面,微信服務器然后給我們一些狀態碼,從而判斷我們的菜單是否創建成功,只是在發送json數據以前我們要做一 些身份驗證。

二.准備工作

首先把我們要創建的菜單寫在一個txt文本中:

 {
     "button":[
       {
            "type":"view",
            "name":"付停車費",
            "url":"http://www.baidu.com"
   
       },{
           "name":"個人中心",
           "sub_button":[
           {    
               "type":"view",
               "name":"個人信息",
               "url":"http://www.baidu.com"
            },
            {
               "type":"view",
               "name":"訂單查詢",
               "url":"http://www.baidu.com"
            },
            {
               "type":"view",
               "name":"使用幫助",
               "url":"http://www.baidu.com"
            },
            {
               "type":"view",
               "name":"下載APP",
               "url":"http://www.baidu.com"
            }]
       }]
 }

三.開始編碼

  首先我們創建一個一般處理程序createMenu.ashx.

        public string  access_token { get; set; }

        protected void Page_Load(object 

sender, EventArgs e)
        {
            FileStream fs1 = new FileStream(Server.MapPath(".") + "\\menu.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("UTF-8"));
            string menu = sr.ReadToEnd();
            sr.Close();
            fs1.Close();
            var str = GetPage("https://api.weixin.qq.com/cgi-bin/token?

grant_type=client_credential&appid=wxd811f5114e3e56f3&secret=76eb33f66129692da16d148cb3c024f1", "");
            JObject jo = JObject.Parse(str);
            access_token = jo["access_token"].ToString();
            GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token + "", menu);
        }

  這里需要注意的是appid,secret這些參數需要換成我們自己的,這些參數我們可以放在配置文件中。也可以單獨的放在一個幫助類里面。

     同時在創建菜單的時候我們需要帶上我的access_token這個令牌來驗證我們的身份,那么我們首先要做的就是獲取我們的這個令牌,那個這個令牌要如何獲取了,我們可以通過一個接口獲取 ,只需要傳遞我們的appid和secret這個兩個參數

{"access_token":"jVLAT9Rp9dNgxI4pb4RWlSx_9HJLXICmk_uWDlRtAug8wcaWhZZ10eqZCYRZrEwCIJf1-vBhS9YEX00Dj7q__lJCyTIWOxTruOd25opkf-0","expires_in":7200}

  上面的GetPage方法的返回值。這樣我們就可以獲取我們的令牌了。

      最后一步:帶上我們的令牌,post我們的json菜單數據就可以創建菜單了。

      當你看到如下代碼:

{"errcode":0,"errmsg":"ok"}

  說明你的菜單創建成功了。

四:GetPage

    代碼如下:

        public string GetPage(string posturl, 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 = "POST";
                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;
                Response.Write(content);
                return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return string.Empty;
            }
        }

五.微信公眾號開發系列

1.0初始微信公眾號

2.0創建自定義菜單

3.0查詢自定義菜單

4.0公眾號消息處理

5.0微信支付

6.0模板消息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM