開發之前,我們需要閱讀官方的接口說明文檔,不得不吐槽一下,微信的這個官方文檔真的很爛,但是,為了開發我們需要的功能,我們也不得不去看這些文檔.接口文檔位址:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html看了這些個文檔,基本意思明白了,就是我們把我們要建立的菜單建立好,post到微信的伺服器上面,微信伺服器然後給我們一些狀态碼,進而判斷我們的菜單是否建立成功,隻是在發送json資料以前我們要做一些身份驗證。
一.前言
開發之前,我們需要閱讀官方的接口說明文檔,不得不吐槽一下,微信的這個官方文檔真的很爛,但是,為了開發我們需要的功能,我們也不得不去看這些文檔.
接口文檔位址: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模闆消息
