一、什么是access_token?
access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。由于获取access_token的api调用次数非常有限,建议开发者全局存储与更新access_token,频繁刷新access_token会导致api调用受限,影响自身业务。
二、要解决的问题
1、如何获取access_token。
2、由于access_token的有效期为7200秒,即2小时,并且重复获取将导致上次获取的access_token失效,获取access_token的api调用次数非常有限,所以要解决如何全局存储与更新access_token。
三、思路
1、将access_token存储在数据库中。
2、何时更新access_token呢?当access_token失效的时候更新,那么怎么判断access_token有没有失效呢?使用当前的access_token请求微信接口,获取自定义菜单,如果返回的errcode为42001,则说明access_token已经失效,这时再重新获取access_token。
四、代码:
1、Http请求代码(HttpRequestUtil类):
- #region 请求Url,不发送数据
-
- /// <summary>
-
- /// 请求Url,不发送数据
-
- /// </summary>
-
- public static string RequestUrl(string url)
-
- {
-
- return RequestUrl(url, "POST");
-
- }
-
- #endregion
-
- #region 请求Url,不发送数据
-
- /// <summary>
-
- /// 请求Url,不发送数据
-
- /// </summary>
-
- public static string RequestUrl(string url, string method)
-
- {
-
- // 设置参数
-
- 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");
-
- //发送请求并获取相应回应数据
-
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
-
- //直到request.GetResponse()程序才开始向目标网页发送Post请求
-
- Stream responseStream = response.GetResponseStream();
-
- StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
-
- //返回结果网页(html)代码
-
- string content = sr.ReadToEnd();
-
- return content;
-
- }
-
- #endregion
2、辅助方法(Tools类):
- namespace SWX.Utils
-
- {
-
- /// <summary>
-
- /// 工具类
-
- /// </summary>
-
- public class Tools
-
- {
-
- #region 获取Json字符串某节点的值
-
- /// <summary>
-
- /// 获取Json字符串某节点的值
-
- /// </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)
-
- {
-
- //先截逗号,若是最后一个,截“}”号,取最小值
-
- int end = jsonStr.IndexOf(',', index);
-
- if (end == -1)
-
- {
-
- end = jsonStr.IndexOf('}', index);
-
- }
-
- result = jsonStr.Substring(index, end - index);
-
- result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格
-
- }
-
- }
-
- return result;
-
- }
-
- #endregion
-
- }
-
- }
3、判断access_token是否过期(WXApi类):
- #region 验证Token是否过期
-
- /// <summary>
-
- /// 验证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、请求微信接口,获取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、全局存储与更新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