/// <summary> /// 通过OAuth 2.0 获取Token /// </summary> /// <param name="appid"></param> /// <param name="appsecret"></param> /// <param name="code"></param> /// <param name="error"></param> /// <returns></returns> public static OAuth2Token GetOpenIdWithOAuth2(string code, out string error) { string msg = string.Empty; OAuth2Token ot = null; try { string url = string.Format(GetTokenFormOauth2Url, Appid, AppSecrat, code); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")); string json = sr.ReadToEnd(); sr.Close(); response.Close(); ot = SerializeUtil.DeserializeJson <OAuth2Token>(json, true); } catch (Exception ex) { msg = ex.Message; } error = msg; return(ot); }
/// <summary> /// 拉取用户信息 /// </summary> public static WxUserInfo GetUserInfo(string access_token, string openid, out string error) { string msg = string.Empty; WxUserInfo userinfo = null; try { string url = string.Format(GetUserInfoUrl, access_token, openid); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")); string json = sr.ReadToEnd(); sr.Close(); response.Close(); userinfo = SerializeUtil.DeserializeJson <WxUserInfo>(json, true); } catch (Exception ex) { msg = ex.Message; } error = msg; return(userinfo); }