欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-30 21:20:45
本文实例为大家分享了asp.net微信粉丝信息接口查看代码,供大家参考,具体内容如下 微信token实体类: /// //...

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

微信token实体类:

 /// <summary>
 /// 微信token实体类
 /// </summary>
 public class wechattokenentity
 {
 public string access_token { get; set; }

 public string expires_in { get; set; }
 }

用户信息实体类

 /// <summary>
 /// 用户实体信息类
 /// </summary>
 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
 {
 /*
  * 步骤:
  * 1.通过appid和secret请求微信url,得到token
  * 2.通过access_token和openid得到用户信息(头像地址等)
  * 3.通过access_token和media_id得到用户发送的微信消息
  * 
  */


 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()
 {

 }

 /// <summary>
 /// 获取token信息
 /// </summary>
 /// <returns></returns>
 public wechattokenentity getwechattoken()
 {
  //请求的url地址
  string tokenurl = string.format(wechaturl, appid, appsecret);
  wechattokenentity mytoken;

  try
  {
  //声明并实例化一个webclient对象
  webclient client = new webclient();
  //从执行url下载数据
  byte[] pagedata = client.downloaddata(tokenurl);
  //把原始数据的byte数组转为字符串
  string jsonstr = encoding.default.getstring(pagedata);
  //初始化一个javascriptserializer json解析器
  //序列化注意:需要引用system.web.extensions
  javascriptserializer jss = new javascriptserializer();
  //将字符串反序列化为token对象
  mytoken = jss.deserialize<wechattokenentity>(jsonstr);
  }
  catch (webexception ex)
  {
  throw ex;
  }
  catch (exception ex)
  {
  throw ex;
  }

  return mytoken;
 }

 /// <summary>
 /// 获取用户信息
 /// </summary>
 /// <param name="accesstoken"></param>
 /// <param name="openid"></param>
 /// <returns></returns>
 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);

  //todo:获取声音
  voiceaddress = jsonstr;

  return voiceaddress;
 }

 /// <summary>
 /// 时间戳转为当前时间
 /// </summary>
 /// <param name="timestamp"></param>
 /// <returns></returns>
 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();
 }
 }

运行结果如图:

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

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

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