微信Token实体类:
public class WeChatTokenEntity
{
public string Access_token { get; set; }
public string Expires_in { get; set; }
}
用户信息实体类
public class WeChatUserEntity
{
public string Subscribe { get; set; }
public string Openid { get; set; }
public string Nickname { get; set; }
public string Sex { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string Country { get; set; }
public string HeadImgUrl { get; set; }
public string Subscribe_time { get; set; }
public string Language { get; set; }
}
微信辅助操作类
public class WeChatDemo
{
string appId = "wxxxxxxxxxxxxxx";
string appSecret = "1234567890-==687";
string wechatUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
public WeChatDemo()
{
}
public WeChatTokenEntity GetWechatToken()
{
string tokenUrl = string.Format(wechatUrl, appId, appSecret);
WeChatTokenEntity myToken;
try
{
WebClient client = new WebClient();
byte[] pageData = client.DownloadData(tokenUrl);
string jsonStr = Encoding.Default.GetString(pageData);
JavaScriptSerializer jss = new JavaScriptSerializer();
myToken = jss.Deserialize<WeChatTokenEntity>(jsonStr);
}
catch (WebException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
return myToken;
}
public WeChatUserEntity GetUserIfo(string accessToken, string openId)
{
WeChatUserEntity wue = new WeChatUserEntity();
string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}";
url = string.Format(url, accessToken, openId);
try
{
WebClient wc = new WebClient();
byte[] pageData = wc.DownloadData(url);
string jsonStr = Encoding.UTF8.GetString(pageData);
JavaScriptSerializer jss = new JavaScriptSerializer();
wue = jss.Deserialize<WeChatUserEntity>(jsonStr);
}
catch (WebException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
return wue;
}
public string GetVoice(string accessToken, string mediaId)
{
string voiceAddress = string.Empty;
string voiceUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}";
voiceUrl = string.Format(voiceUrl, accessToken, mediaId);
WebClient wc = new WebClient();
byte[] pageData = wc.DownloadData(voiceUrl);
string jsonStr = Encoding.UTF8.GetString(pageData);
voiceAddress = jsonStr;
return voiceAddress;
}
public DateTime TimeStamp2DateTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long time = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(time);
return dtStart.Add(toNow);
}
}
主程序:
class Program
{
static void Main(string[] args)
{
WeChatDemo wcd = new WeChatDemo()
WeChatTokenEntity wte = wcd.GetWechatToken()
string token = wte.Access_token
string openId = "ogNVpt52xxxxxxxxxxxxxxxxxx"
Console.WriteLine("第一步:获得access_token:\n " + token + "\n")
Console.WriteLine("第二步:获得用户信息")
WeChatUserEntity user = wcd.GetUserIfo(token, openId)
Console.WriteLine("\n昵称:" + user.Nickname)
Console.WriteLine("国家:" + user.Country)
Console.WriteLine("省份:" + user.Province)
Console.WriteLine("城市:" + user.City)
Console.WriteLine("语言:" + user.Language)
Console.WriteLine("性别:" + user.Sex)
Console.WriteLine("OpenId:" + user.Openid)
Console.WriteLine("是否订阅:" + user.Subscribe)
Console.WriteLine("时间:" + wcd.TimeStamp2DateTime(user.Subscribe_time))
Console.WriteLine("头像地址:" + user.HeadImgUrl)
Console.WriteLine("\n第三步:获取微信声音地址")
string mediaId = "vwvnskvsldkvmsdlvkmdslkvmsld"
string voiceAddress = wcd.GetVoice(token, mediaId)
Console.WriteLine("声音地址:" + voiceAddress)
Console.Read()
}
}
运行结果如图: