開發(fā)之前,我們需要閱讀官方的接口說明文檔,不得不吐槽一下,微信的這個(gè)官方文檔真的很爛,但是,為了開發(fā)我們需要的功能,我們也不得不去看這些文檔.
接口文檔地址:http://mp.weixin.QQ.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html
看了這些個(gè)文檔,基本意思明白了,就是我們把我們要?jiǎng)?chuàng)建的菜單創(chuàng)建好,post到微信的服務(wù)器上面,微信服務(wù)器然后給我們一些狀態(tài)碼,從而判斷我們的菜單是否創(chuàng)建成功,只是在發(fā)送json數(shù)據(jù)以前我們要做一 些身份驗(yàn)證。
首先把我們要?jiǎng)?chuàng)建的菜單寫在一個(gè)txt文本中:
{ "button":[ { "type":"view", "name":"付停車費(fèi)", "url":"http://www.baidu.com" },{ "name":"個(gè)人中心", "sub_button":[ { "type":"view", "name":"個(gè)人信息", "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" }] }] }
首先我們創(chuàng)建一個(gè)一般處理程序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這些參數(shù)需要換成我們自己的,這些參數(shù)我們可以放在配置文件中。也可以單獨(dú)的放在一個(gè)幫助類里面。
同時(shí)在創(chuàng)建菜單的時(shí)候我們需要帶上我的access_token這個(gè)令牌來驗(yàn)證我們的身份,那么我們首先要做的就是獲取我們的這個(gè)令牌,那個(gè)這個(gè)令牌要如何獲取了,我們可以通過一個(gè)接口獲取 ,只需要傳遞我們的appid和secret這個(gè)兩個(gè)參數(shù)
{"access_token":"jVLAT9Rp9dNgxI4pb4RWlSx_9HJLXICmk_uWDlRtAug8wcaWhZZ10eqZCYRZrEwCIJf1-vBhS9YEX00Dj7q__lJCyTIWOxTruOd25opkf-0","expires_in":7200}上面的GetPage方法的返回值。這樣我們就可以獲取我們的令牌了。
最后一步:帶上我們的令牌,post我們的json菜單數(shù)據(jù)就可以創(chuàng)建菜單了。
當(dāng)你看到如下代碼:
{"errcode":0,"errmsg":"ok"}說明你的菜單創(chuàng)建成功了。
代碼如下:
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); // 準(zhǔn)備請求... try { // 設(shè)置參數(shù) 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(); //發(fā)送請求并獲取相應(yīng)回應(yīng)數(shù)據(jù) response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁發(fā)送Post請求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回結(jié)果網(wǎng)頁(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創(chuàng)建自定義菜單
3.0查詢自定義菜單
4.0公眾號消息處理
5.0微信支付
6.0模板消息
新聞熱點(diǎn)
疑難解答
圖片精選