微信公众平台开发 C#如何获取到access token的简单例子
access_token是微信公众号的全局唯一接口调用凭据,公众号开发调用各接口时都需使用access_token。
此篇内容记录了调用access_token接口调用凭据的完整C#例子
官方接口API文档:
https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html#
文档有请求说明返回结果是json格式的结果
///接口正常返回的结果 { "access_token":"ACCESS_TOKEN", "expires_in":7200 }
///接口请求错误是返回结果 { "errcode":40013, "errmsg":"invalid appid" }
公共基类:因为所有接口的错误格式是通用的 所以定义一个公共错误实体类 (用于所有接口结果类用来作为基类继承)
namespace YuanTK.WeiXin.Wrapper { ////// 接口错误信息 接口基类 /// public class ErrorMessage { /// /// 错误码 /// public int errcode { get; set; } /// /// 错误信息 /// public string errmsg { get; set; } } }
根据这个 AccessToken接口返回的 json字符串,定义AccessTokenResponse类 并继承自 ErrorMessage
namespace YuanTK.WeiXin.Wrapper { ////// 凭证接口输出结果 /// public class AccessTokenResponse: ErrorMessage { /// /// 获取到的凭证 /// public string access_token { get; set; } /// /// 凭证有效时间,单位:秒 /// public string expires_in { get; set; } } }
定义一个IWeiXin 泛型接口类 泛型约束ErrorMessage
namespace YuanTK.WeiXin { ////// 定义微信通用接口 /// /// public interface IWeiXin where T: ErrorMessage { /// /// 输出结果 /// /// T GetResponse(); } }
创建WeiXinRequest 接口请求公共方法类 泛型约束 ErrorMessage
namespace YuanTK.WeiXin.Common { ////// 微信接口请求方法 /// public class WeiXinRequest where T : ErrorMessage { /// /// Get请求 /// /// /// /// public T Get(string url) { HttpWebRequest ApiRequest = (HttpWebRequest)WebRequest.Create(url); ApiRequest.Method = "GET";//post方式 ApiRequest.Accept = "text/html, application/xhtml+xml, */*"; ApiRequest.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response = (HttpWebResponse)ApiRequest.GetResponse(); T TResponse = default(T); using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8)) { string jsonStr = reader.ReadToEnd(); TResponse = Newtonsoft.Json.JsonConvert.DeserializeObject (jsonStr); } return TResponse; } /// /// POST请求 /// /// /// /// /// public T Post(string url, string poststr) { HttpWebRequest ApiRequest = (HttpWebRequest)WebRequest.Create(url); ApiRequest.Method = "POST";//post方式 ApiRequest.Accept = "text/html, application/xhtml+xml, */*"; ApiRequest.ContentType = "application/x-www-form-urlencoded"; if (!string.IsNullOrEmpty(poststr)) { byte[] buffer = Encoding.UTF8.GetBytes(poststr); ApiRequest.ContentLength = buffer.Length; ApiRequest.GetRequestStream().Write(buffer, 0, buffer.Length); } HttpWebResponse response = (HttpWebResponse)ApiRequest.GetResponse(); T TResponse = default(T); using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8)) { string jsonStr = reader.ReadToEnd(); TResponse = Newtonsoft.Json.JsonConvert.DeserializeObject (jsonStr); } return TResponse; } } }
基本的公共类或方法类 准备好了 再定义一个AccessToken类方法继承自 WeiXinRequest 并绑定接口 IWeiXin
namespace YuanTK.WeiXin { ////// AccessToken接口 /// public class AccessToken : WeiXinRequest ,IWeiXin { String api; public AccessToken(string appid, string secret) { api = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}"; } /// /// 调用接口获取结果 /// /// public AccessTokenResponse GetResponse() { return this.Get(api); } } }
微信公众号接口调用access_token凭据使用例子
//微信公众号平台分配的appid string appid = "wx*3dc7*************"; //微信公众号平台分配的appid string secret = "***bca72d5bbb***********"; //初始化接口方法 IWeiXinapi = new AccessToken(appid, secret); //调用接口并输出结果 AccessTokenResponse token = api.GetResponse();
获取成功效果图