Home > Article > WeChat Applet > C# WeChat Development Series-Getting Credentials for Interface Calling
3.0 Obtain interface call credentials
①Interface description
access_token is the globally unique ticket of the official account. When the official account calls each interface All require access_token. Developers need to store it properly. At least 512 characters of space must be reserved for access_token storage. The validity period of access_token is currently 2h (7200s) and needs to be refreshed regularly. Repeated acquisition will cause the last access_token to become invalid.
Instructions on the use and generation of access_token required for API calls on the public platform:
1. In order to keep appsecrect confidential, the third party needs an access_token to obtain and Refreshed central control server. The access_token used by other business logic servers comes from the central control server and should not be refreshed individually, otherwise the access_token will be overwritten and affect the business;
2. The current validity period of the access_token is conveyed through the returned expire_in , currently the value within 7200 seconds. The central control server needs to refresh the new access_token in advance according to this valid time. During the refresh process, the central control server still outputs the old access_token. At this time, the public platform backend will ensure that both the old and new access_token are available within a short time of refresh, which ensures the smooth transition of third-party services;
3. The validity time of access_token may be adjusted in the future, so the central control server not only needs to actively refresh internally at regular intervals, but also needs to provide an interface for passively refreshing access_token, so that the business server can know that the access_token has been updated during API calls. In the case of timeout, the refresh process of access_token can be triggered.
How to get AppID and AppSecret! ?
The public account can use AppID and AppSecret to call this interface to obtain access_token. AppID and AppSecret can be obtained from the official website of WeChat public platform - Developer Center page. (You need to have become a developer, and the account has no abnormal status)
Note: Use the https protocol when calling all WeChat interfaces; also, if the third party does not use the central control server, but selects each business If each logical point refreshes access_taken, conflicts may occur, resulting in service instability.
②Request interface
Interface call request description:
/// <summary> /// 获取公众号的ACCESS_TOKEN /// </summary> /// <returns>返回操作凭据</returns> public string GetAccessToken() { if (HttpContext.Current.Cache["access_token"] == null) { string para = string.Format("grant_type=client_credential&appid={0}&secret={1}", AppID, AppSecret); string results = SendHTTPRequest("POST", "https://api.weixin.qq.com/cgi-bin/token", para); JObject obj = (JObject)JsonConvert.DeserializeObject(results); //*******************************设置access_token的过期机制************************** Cache cache = HttpContext.Current.Cache; cache.Insert("access_token", obj["access_token"].ToString(), null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration); //******************************************end************************************* return HttpContext.Current.Cache["access_token"].ToString(); } else { return HttpContext.Current.Cache["access_token"].ToString(); } }Return parameter description: Return instructions: