首页 > 微信 > 详细

C#获取微信公众号的access_token

时间:2020-02-21 20:00:09      阅读:265      评论:0      收藏:0      [点我收藏+]
        static public string access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        static public string GetAccessToken(string appid, string appsecret)
        {
            string requestUrl = access_token_url.Replace("APPID", appid).Replace("APPSECRET", appsecret);
            WebResponse result = null;
            WebRequest req = WebRequest.Create(requestUrl);
            result = req.GetResponse();
            Stream s = result.GetResponseStream();
            XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(s, XmlDictionaryReaderQuotas.Max);
            xmlReader.Read();
            string xml = xmlReader.ReadOuterXml();
            s.Close();
            s.Dispose();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlElement rootElement = doc.DocumentElement;
            string access_token = rootElement.SelectSingleNode("access_token").InnerText.Trim();
            return access_token;
        }

 

C#获取微信公众号的access_token

原文:https://www.cnblogs.com/hofmann/p/12342340.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!