public static WebchatJsUserinfo GetUserInfo(string userAgent, string CODE) { WechatConfig wechatconfig = AccessTokenService.GetWechatConfig(); WebchatJsUserinfo userinfo = new WebchatJsUserinfo(); string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wechatconfig.Appid + "&secret=" + wechatconfig.AppSecret + "&code=" + CODE + "&grant_type=authorization_code"; HttpWebResponse response = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null); Stream stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream); string result = sr.ReadToEnd(); WechatJsToken token = JsonConvert.DeserializeObject <WechatJsToken>(result); string url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token.access_token + "&openid=" + token.openid + "&lang=zh_CN"; HttpWebResponse res = WechatHttpWebResponseUtility.CreateGetHttpResponse(url2, null, userAgent, null); Stream stream2 = res.GetResponseStream(); StreamReader sr2 = new StreamReader(stream2); string result2 = sr2.ReadToEnd(); userinfo = JsonConvert.DeserializeObject <WebchatJsUserinfo>(result2); return(userinfo); }
public static WechatConfig GetWechatConfig() { Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); AppSettingsSection appsection = config.GetSection("appSettings") as AppSettingsSection; WechatConfig wechatConfig = new WechatConfig(); wechatConfig.Appid = appsection.Settings["AppID"].Value; wechatConfig.AppSecret = appsection.Settings["AppSecret"].Value; wechatConfig.AccessToken = appsection.Settings["WechatAccessToken"].Value; wechatConfig.AccessTokenExpiredTime = appsection.Settings["WechatAccessTokenExpiredTime"].Value; return(wechatConfig); }
//获取accesstoken public static string GetAccessToken() { string access_token = string.Empty; WechatConfig wechatConfig = AccessTokenService.GetWechatConfig(); if (DateTime.Now < DateTime.Parse(wechatConfig.AccessTokenExpiredTime)) { access_token = wechatConfig.AccessToken; } else { access_token = AccessTokenService.RefrenshToken(wechatConfig.Appid, wechatConfig.AppSecret); } return(access_token); }