程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> ASP.NET微信公眾號查看粉絲信息接口,asp.net粉絲

ASP.NET微信公眾號查看粉絲信息接口,asp.net粉絲

編輯:關於.NET

ASP.NET微信公眾號查看粉絲信息接口,asp.net粉絲


本文實例為大家分享了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微信開發教程匯總》,歡迎大家學習閱讀。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持幫客之家。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved