C#微信公眾平臺(tái)開發(fā)之a(chǎn)ccess
一、什么是access_token?
access_token是公眾號(hào)的全局唯一票據(jù),公眾號(hào)調(diào)用各接口時(shí)都需使用access_token。正常情況下access_token有效期為7200秒,重復(fù)獲取將導(dǎo)致上次獲取的access_token失效。由于獲取access_token的api調(diào)用次數(shù)非常有限,建議開發(fā)者全局存儲(chǔ)與更新access_token,頻繁刷新access_token會(huì)導(dǎo)致api調(diào)用受限,影響自身業(yè)務(wù)。
二、要解決的問題
1、如何獲取access_token。
2、由于access_token的有效期為7200秒,即2小時(shí),并且重復(fù)獲取將導(dǎo)致上次獲取的access_token失效,獲取access_token的api調(diào)用次數(shù)非常有限,所以要解決如何全局存儲(chǔ)與更新access_token。
三、思路
1、將access_token存儲(chǔ)在數(shù)據(jù)庫(kù)中。
2、何時(shí)更新access_token呢?當(dāng)access_token失效的時(shí)候更新,那么怎么判斷access_token有沒有失效呢?使用當(dāng)前的access_token請(qǐng)求微信接口,獲取自定義菜單,如果返回的errcode為42001,則說明access_token已經(jīng)失效,這時(shí)再重新獲取access_token。
數(shù)據(jù)庫(kù)設(shè)計(jì)(表名SWX_Config):
四、代碼:
1、Http請(qǐng)求代碼(HttpRequestUtil類):
#region 請(qǐng)求Url,不發(fā)送數(shù)據(jù) /// <summary> /// 請(qǐng)求Url,不發(fā)送數(shù)據(jù) /// </summary> public static string RequestUrl(string url) { return RequestUrl(url, "POST"); } #endregion #region 請(qǐng)求Url,不發(fā)送數(shù)據(jù) /// <summary> /// 請(qǐng)求Url,不發(fā)送數(shù)據(jù) /// </summary> public static string RequestUrl(string url, string method) { // 設(shè)置參數(shù) HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = method; request.ContentType = "text/html"; request.Headers.Add("charset", "utf-8"); //發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù) HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁(yè)發(fā)送Post請(qǐng)求 Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.UTF8); //返回結(jié)果網(wǎng)頁(yè)(html)代碼 string content = sr.ReadToEnd(); return content; } #endregion
2、輔助方法(Tools類):
namespace SWX.Utils { /// <summary> /// 工具類 /// </summary> public class Tools { #region 獲取Json字符串某節(jié)點(diǎn)的值 /// <summary> /// 獲取Json字符串某節(jié)點(diǎn)的值 /// </summary> public static string GetJsonValue(string jsonStr, string key) { string result = string.Empty; if (!string.IsNullOrEmpty(jsonStr)) { key = "\"" + key.Trim('"') + "\""; int index = jsonStr.IndexOf(key) + key.Length + 1; if (index > key.Length + 1) { //先截逗號(hào),若是最后一個(gè),截“}”號(hào),取最小值 int end = jsonStr.IndexOf(',', index); if (end == -1) { end = jsonStr.IndexOf('}', index); } result = jsonStr.Substring(index, end - index); result = result.Trim(new char[] { '"', ' ', '\'' }); //過濾引號(hào)或空格 } } return result; } #endregion } }
3、判斷access_token是否過期(WXApi類):
#region 驗(yàn)證Token是否過期 /// <summary> /// 驗(yàn)證Token是否過期 /// </summary> public static bool TokenExpired(string access_token) { string jsonStr = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", access_token)); if (Tools.GetJsonValue(jsonStr, "errcode") == "42001") { return true; } return false; } #endregion
4、請(qǐng)求微信接口,獲取access_token(WXApi類):
#region 獲取Token /// <summary> /// 獲取Token /// </summary> public static string GetToken(string appid, string secret) { string strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret)); return Tools.GetJsonValue(strJson, "access_token"); } #endregion
5、全局存儲(chǔ)與更新access_token(AdminUtil類):
#region 獲取access_token /// <summary> /// 獲取access_token /// </summary> public static string GetAccessToken(PageBase page) { string access_token = string.Empty; UserInfo user = GetLoginUser(page); if (user != null) { if (string.IsNullOrWhiteSpace(user.access_token)) //尚未保存過access_token { access_token = WXApi.GetToken(user.AppID, user.AppSecret); } else { if (WXApi.TokenExpired(user.access_token)) //access_token過期 { access_token = WXApi.GetToken(user.AppID, user.AppSecret); } else { return user.access_token; } } MSSQLHelper.ExecuteSql(string.Format("update SWX_Config set access_token='{0}' where UserName='{1}'", access_token, user.UserName)); } return access_token; } #endregion
精彩專題分享:ASP.NET微信開發(fā)教程匯總,歡迎大家學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家進(jìn)行微信公眾平臺(tái)開發(fā)有所幫助。
上一篇:C#利用時(shí)間和隨即字符串創(chuàng)建唯一的訂單編號(hào)
欄 目:C#教程
下一篇:基于C# 生成Zip壓縮包代碼
本文標(biāo)題:C#微信公眾平臺(tái)開發(fā)之a(chǎn)ccess
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6606.html
您可能感興趣的文章
- 01-10微信開放平臺(tái)之網(wǎng)站授權(quán)微信登錄功能
- 01-10C#微信開發(fā)(服務(wù)器配置)
- 01-10C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口
- 01-10C#微信開發(fā)之微信公眾號(hào)標(biāo)簽管理功能
- 01-10C#微信公眾號(hào)與訂閱號(hào)接口開發(fā)示例代碼
- 01-10C#微信開發(fā)第一章
- 01-10C#實(shí)現(xiàn)的微信網(wǎng)頁(yè)授權(quán)操作邏輯封裝示例
- 01-10WPF微信聊天和通訊錄按鈕樣式代碼分享
- 01-10使用TypeScript開發(fā)微信小程序的方法
- 01-10c#版在pc端發(fā)起微信掃碼支付的實(shí)例


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載