/// <summary> /// 获取调用JS SDK时所需的access_token /// 文档地址:http://mp.weixin.qq.com/wiki/15/54ce45d8d30b6bf6758f68d2e95bc627.html /// </summary> /// <returns></returns> public static string GetAccessToken() { var cache = new AspNetCache(); if (cache.Get <string>("access_token") == null) { string errmsg; string appid = ConfigurationManager.AppSettings["AppId"]; string appsecret = ConfigurationManager.AppSettings["AppSecret"]; string apiUrl = string.Format( "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, appsecret); string responseStr = Get(apiUrl, out errmsg, null); if (responseStr != null) { //var dic = responseStr.JSONDeserialize<Dictionary<string, object>>(); var dic = JsonConvert.DeserializeObject <Dictionary <string, object> >(responseStr); if (dic.ContainsKey("access_token")) { cache.Add("access_token", dic["access_token"].ToString()); token = dic["access_token"].ToString(); return(dic["access_token"].ToString()); } } } else { return(cache.Get <string>("access_token")); } return(null); }
/// <summary> /// 获取调用JS SDK时所需的票据 /// 文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95 /// </summary> /// <returns></returns> public static string GetJsApiTicket() { var cache = new AspNetCache(); if (cache.Get <string>("ticket") == null) { string errmsg; string apiUrl = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", GetAccessToken()); string responseStr = Get(apiUrl, out errmsg, null); if (responseStr != null) { //var dic = responseStr.JSONDeserialize<Dictionary<string, object>>(); var dic = JsonConvert.DeserializeObject <Dictionary <string, object> >(responseStr); if (dic.ContainsKey("ticket")) { cache.Add("ticket", dic["ticket"].ToString()); return(dic["ticket"].ToString()); } } } else { return(cache.Get <string>("ticket")); } return(null); }