public string GetUserInfo(string openid) { var access_token = GetAccessToken(); var jsonStr = WeChatApi.GetUserInfo(access_token, openid); return(jsonStr); }
/// <summary> /// 获取OpenID /// </summary> public string GetOpenID(string redirect_url, string code) { string openid = ""; openid = WeChatApi.GetOpenID(_appId, redirect_url, code, _appSecret); return(openid); }
/// <summary> /// 获取access_token /// </summary> public string GetAccessToken() { string Token = string.Empty; DateTime validDate; // 读取XML文件 string filepath = "Cache/token.xml"; StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8); XmlDocument xml = new XmlDocument(); xml.Load(str); str.Close(); str.Dispose(); Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText; validDate = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_endTime").InnerText); if (DateTime.Now > validDate) { DateTime _youxrq = DateTime.Now; Access_token mode = WeChatApi.GetAccessTokenFromServer(_appId, _appSecret);//超过有效期,需重新获取 xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token; _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in)); xml.SelectSingleNode("xml").SelectSingleNode("Access_endTime").InnerText = _youxrq.ToString(); xml.Save(filepath); Token = mode.access_token; } return(Token); }
public static Access_token GetAccessTokenFromServer(string appid, string appSecret) { string jsonStr = WeChatApi.HttpGet(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, appSecret)); Access_token token = new Access_token(); token = JsonHelper.ParseFromJson <Access_token>(jsonStr); return(token); }