C#微信公衆號開發——access_token的獲取

access_token是公衆號的全局惟一票據,公衆號調用各接口時都需使用access_token。正常狀況下access_token有效期爲7200秒,重複獲取將致使上次獲取的access_token失效。api

公衆號可使用AppID和AppSecret調用本接口來獲取access_token。AppID和AppSecret可在微信公衆平臺官網-開發者中心頁中得到(須要已經成爲開發者,且賬號沒有異常狀態)。注意調用全部微信接口時均需使用https協議。微信

獲取access_token的思路是:用XML文檔存放access_token信息,獲取access_token的時候根據XML存放的上次生成時間判斷是否過時,若是過時從新獲取。app

1、新建一個XML文檔,用來存放access_token和時間信息微信公衆平臺

<?xml version="1.0" encoding="utf-8"?>
<xml>
  <Access_Token>存放Access_Toke,新建的時候能夠隨便寫,後面調用的時候會更新的</Access_Token>
  <Access_YouXRQ>2016/7/22 16:13:15</Access_YouXRQ>
</xml>

2、獲取access_token,appid和secret須要輸入本身微信公衆號的就能夠了spa

        /// <summary>
        /// 經過appID和appsecret獲取Access_token
        /// </summary>
        /// <returns></returns>
        private static Access_token GetAccess_token()
        {
            string appid = "appid";
            string secret = "appsecret";
            string strUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
            Access_token mode = new Access_token();
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl);
            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string content = reader.ReadToEnd();//在這裏對Access_token 賦值  
                Access_token token = new Access_token();
                token = JsonHelper.ParseFromJson<Access_token>(content);
                mode.access_token = token.access_token;
                mode.expires_in = token.expires_in;
            }
            return mode;
        }

3、因爲access_token會過時,因此獲取access_token的時候要驗證是否過時了code

        /// <summary>
        /// 獲取Access_token值
        /// </summary>
        /// <returns></returns>
        public static string IsExistAccess_Token()
        {
            string Token = string.Empty;
            DateTime YouXRQ;
            // 讀取XML文件中的數據,並顯示出來 ,注意文件路徑  
            string filepath = System.Web.HttpContext.Current.Server.MapPath("XMLFile.xml");
            StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
            XmlDocument xml = new XmlDocument();
            xml.Load(str);
            str.Close();
            str.Dispose();
            Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
            YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);
            if (DateTime.Now > YouXRQ)
            {
                DateTime _youxrq = DateTime.Now;
                Access_token mode = GetAccess_token();
                xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
                _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in));
                xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
                xml.Save(filepath);
                Token = mode.access_token;
            }
            return Token;
        }

4、直接調用IsExistAccess_Token()就能夠獲取了xml

string AccessToken = IsExistAccess_Token();
相關文章
相關標籤/搜索