当前位置:Gxlcms > asp.net > ASP.NET微信公众号查看粉丝信息接口

ASP.NET微信公众号查看粉丝信息接口

时间:2021-07-01 10:21:17 帮助过:8人阅读

本文实例为大家分享了ASP.NET微信粉丝信息接口查看代码,供大家参考,具体内容如下

微信Token实体类:

  1. /// <summary>
  2. /// 微信Token实体类
  3. /// </summary>
  4. public class WeChatTokenEntity
  5. {
  6. public string Access_token { get; set; }
  7. public string Expires_in { get; set; }
  8. }

用户信息实体类

  1. /// <summary>
  2. /// 用户实体信息类
  3. /// </summary>
  4. public class WeChatUserEntity
  5. {
  6. public string Subscribe { get; set; }
  7. public string Openid { get; set; }
  8. public string Nickname { get; set; }
  9. public string Sex { get; set; }
  10. public string City { get; set; }
  11. public string Province { get; set; }
  12. public string Country { get; set; }
  13. public string HeadImgUrl { get; set; }
  14. public string Subscribe_time { get; set; }
  15. public string Language { get; set; }
  16. }

微信辅助操作类

  1. public class WeChatDemo
  2. {
  3. /*
  4. * 步骤:
  5. * 1.通过appid和secret请求微信url,得到token
  6. * 2.通过access_token和openid得到用户信息(头像地址等)
  7. * 3.通过access_token和media_id得到用户发送的微信消息
  8. *
  9. */
  10. string appId = "wxxxxxxxxxxxxxx";
  11. string appSecret = "1234567890-==687";
  12. string wechatUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
  13. public WeChatDemo()
  14. {
  15. }
  16. /// <summary>
  17. /// 获取token信息
  18. /// </summary>
  19. /// <returns></returns>
  20. public WeChatTokenEntity GetWechatToken()
  21. {
  22. //请求的url地址
  23. string tokenUrl = string.Format(wechatUrl, appId, appSecret);
  24. WeChatTokenEntity myToken;
  25. try
  26. {
  27. //声明并实例化一个WebClient对象
  28. WebClient client = new WebClient();
  29. //从执行url下载数据
  30. byte[] pageData = client.DownloadData(tokenUrl);
  31. //把原始数据的byte数组转为字符串
  32. string jsonStr = Encoding.Default.GetString(pageData);
  33. //初始化一个JavaScriptSerializer json解析器
  34. //序列化注意:需要引用System.Web.Extensions
  35. JavaScriptSerializer jss = new JavaScriptSerializer();
  36. //将字符串反序列化为Token对象
  37. myToken = jss.Deserialize<WeChatTokenEntity>(jsonStr);
  38. }
  39. catch (WebException ex)
  40. {
  41. throw ex;
  42. }
  43. catch (Exception ex)
  44. {
  45. throw ex;
  46. }
  47. return myToken;
  48. }
  49. /// <summary>
  50. /// 获取用户信息
  51. /// </summary>
  52. /// <param name="accessToken"></param>
  53. /// <param name="openId"></param>
  54. /// <returns></returns>
  55. public WeChatUserEntity GetUserIfo(string accessToken, string openId)
  56. {
  57. WeChatUserEntity wue = new WeChatUserEntity();
  58. string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}";
  59. url = string.Format(url, accessToken, openId);
  60. try
  61. {
  62. WebClient wc = new WebClient();
  63. byte[] pageData = wc.DownloadData(url);
  64. string jsonStr = Encoding.UTF8.GetString(pageData);
  65. JavaScriptSerializer jss = new JavaScriptSerializer();
  66. wue = jss.Deserialize<WeChatUserEntity>(jsonStr);
  67. }
  68. catch (WebException ex)
  69. {
  70. throw ex;
  71. }
  72. catch (Exception ex)
  73. {
  74. throw ex;
  75. }
  76. return wue;
  77. }
  78. public string GetVoice(string accessToken, string mediaId)
  79. {
  80. string voiceAddress = string.Empty;
  81. string voiceUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}";
  82. voiceUrl = string.Format(voiceUrl, accessToken, mediaId);
  83. WebClient wc = new WebClient();
  84. byte[] pageData = wc.DownloadData(voiceUrl);
  85. string jsonStr = Encoding.UTF8.GetString(pageData);
  86. //TODO:获取声音
  87. voiceAddress = jsonStr;
  88. return voiceAddress;
  89. }
  90. /// <summary>
  91. /// 时间戳转为当前时间
  92. /// </summary>
  93. /// <param name="timeStamp"></param>
  94. /// <returns></returns>
  95. public DateTime TimeStamp2DateTime(string timeStamp)
  96. {
  97. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  98. long time = long.Parse(timeStamp + "0000000");
  99. TimeSpan toNow = new TimeSpan(time);
  100. return dtStart.Add(toNow);
  101. }
  102. }

主程序:

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. WeChatDemo wcd = new WeChatDemo();
  6. WeChatTokenEntity wte = wcd.GetWechatToken();
  7. string token = wte.Access_token;
  8. string openId = "ogNVpt52xxxxxxxxxxxxxxxxxx";
  9. Console.WriteLine("第一步:获得access_token:\n " + token + "\n");
  10. Console.WriteLine("第二步:获得用户信息");
  11. WeChatUserEntity user = wcd.GetUserIfo(token, openId);
  12. Console.WriteLine("\n昵称:" + user.Nickname);
  13. Console.WriteLine("国家:" + user.Country);
  14. Console.WriteLine("省份:" + user.Province);
  15. Console.WriteLine("城市:" + user.City);
  16. Console.WriteLine("语言:" + user.Language);
  17. Console.WriteLine("性别:" + user.Sex);
  18. Console.WriteLine("OpenId:" + user.Openid);
  19. Console.WriteLine("是否订阅:" + user.Subscribe);
  20. Console.WriteLine("时间:" + wcd.TimeStamp2DateTime(user.Subscribe_time));
  21. Console.WriteLine("头像地址:" + user.HeadImgUrl);
  22. Console.WriteLine("\n第三步:获取微信声音地址");
  23. string mediaId = "vwvnskvsldkvmsdlvkmdslkvmsld";
  24. string voiceAddress = wcd.GetVoice(token, mediaId);
  25. Console.WriteLine("声音地址:" + voiceAddress);
  26. Console.Read();
  27. }
  28. }

运行结果如图:

本文已被整理到了《ASP.NET微信开发教程汇总》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行