微信公众号查看粉丝信息接口
来源:互联网 发布:商客软件 编辑:程序博客网 时间:2024/05/02 04:42
微信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(); } }
运行结果如图:
0 0
- 微信公众号查看粉丝信息接口
- 如何查看别人的微信公众号的粉丝数
- 微信公众号查看关注者接口
- 微信公众号粉丝数增加技巧
- java 获取微信公众号粉丝的openId
- 微信公众号开放接口推送图文信息教程
- 微信公众号调用客服接口发送信息
- 微信公众号接口配置信息失败 tp3.2
- 微信公众号怎么管理粉丝用户-微信公众号使用教程13
- 小C公众帮 获取微信公众号粉丝openid
- 微信支付配置信息,JSAPI接口,H5调用微信js接口支付,微信公众号支付
- 网页中获取微信粉丝信息
- 微信公众号如何成为粉丝1万大v
- 微信公众号2个月如何粉丝过5万?
- 微信营销诀窍:真实粉丝和有效听众才是公众号推广的必备要求
- 如何推广微信公众号(快速增加粉丝数量)?
- 微信投票活动为何能帮助公众号粉丝增长?
- 分享微信公众号运营助手,可以在手机上回复粉丝留言
- 程序设计语言理解
- No orientation specified, and the default is horizontal. This is a common source of bugs
- 【Github教程】史上最全github使用方法:github入门到精通之二
- 中文依存句法分析概述及应用
- java中自己实现一个服务端对应多个客户端的简单代码
- 微信公众号查看粉丝信息接口
- python 信息同时输出到控制台与文件
- UNIX网络编程笔记 第五章 TCP客户/服务器程序示例
- 打印字符串数组中每个字符串出现的次数(字符串与次数对应)
- SwipeBackActivity Activity退出动画和系统动画冲突
- java中对图象文件的处理(读写以及转换)
- 7. Eclipse4 RCP Application不得不掌握的概念
- 【DSP开发】【Linux开发】基于ARM+DSP进行应用开发
- 行为树