This article mainly introduces you in detail. net development to solve the problem of access_token expiration, interested friends can refer to this article for details. net development to solve the problem of access_token expiration, interested friends can refer
Because access_token is frequently used in later advanced functions, we have to modify the access_token mentioned above.
In addition, it should be noted that the access_token is changed and has its own cycle. The official explanation is: "The validity period is 7200 seconds ", this requires us to store the obtained access_token in a physical file or Application, request to modify the content after expiration, and read it when necessary.
Some people may think that if I get one after expiration, I can achieve the same effect without using physical files and applications, however, you must note that the platform also limits the number of access_token requests each day. if there are too many access_token requests for a single user, the number of times exceeds the limit. So we still follow the above ideas to implement these functions: before that, we have learned how to obtain the access_token (connection). now we only need to ensure that it is updated at any time.
First create an Access_token class
////// Summary of Access_token ///Public class Access_token {public Access_token () {// TODO: add the constructor logic here //} string _ access_token; string _ expires_in ;////// The obtained credential ///Public string access_token {get {return _ access_token;} set {_ access_token = value ;}}////// Valid time of the credential, in seconds ///Public string expires_in {get {return _ expires_in;} set {_ expires_in = value ;}}}
Use the following XML file to store access_token and create an XMLFile. xml: write the content of the Access_YouXRQ tag as a time that has passed. in this way, when we call the tag at the beginning, we find that the tag has expired and obtain a new access_token.
You can write the initial values as needed.1980/12/12 16:06:38
Obtain the Access_token method and assign a value to the Access_token instance.
Public static Access_token GetAccess_token () {string appid = your appid; string secret = your secret; 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 (); // Response. write (content); // here Access_token is assigned Access_token token = new Access_token (); token = JsonHelper. parseFromJson (content); mode. access_token = token. access_token; mode. expires_in = token. expires_in;} return mode ;}
The above method uses the processing of Json objects, so I paste the JsonHelper code for your reference. here is the JsonHelper. cs code:
Using System; using System. IO; using System. Text; using System. Runtime. Serialization. Json; public class JsonHelper {////// Generate Json format //////
//////
Public static string GetJson
(T obj) {DataContractJsonSerializer json = new DataContractJsonSerializer (obj. getType (); using (MemoryStream stream = new MemoryStream () {json. writeObject (stream, obj); string szJson = Encoding. UTF8.GetString (stream. toArray (); return szJson ;}}///
/// Obtain the Json Model //////
///
///
Public static T ParseFromJson
(String szJson) {T obj = Activator. CreateInstance
(); Using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (szJson) {DataContractJsonSerializer serializer = new DataContractJsonSerializer (obj. getType (); return (T) serializer. readObject (MS );}}}
We also need a method to determine whether the access_token expires and update the XML file if it expires.
////// Determine whether the Access_Token is out of date based on the current date. if a new Access_Token is returned during the out-of-date period, the previous Access_Token is returned /////////
Public static string IsExistAccess_Token () {string Token = string. empty; DateTime YouXRQ; // read and display the data in the XML file. Note that the file path is string filepath = 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 ;}
Okay. after completing the above work, I just need to call the access_token as follows. "the customer no longer has to worry about token expiration"
String _ access_token = IsExistAccess_Token ();
The above is the details about how to solve the access_token expiration problem in. Net development. For more information, see other related articles on php!