C# WeChat Development Series-Getting Credentials for Interface Calling

高洛峰
Release: 2017-03-03 09:26:37
Original
1619 people have browsed it

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:


##http request method :GET

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

Interface parameter configuration:

C# WeChat Development Series-Getting Credentials for Interface Calling

The specific implementation code is as follows:

/// <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();
   }
}
Copy after login

Return parameter description:

C# WeChat Development Series-Getting Credentials for Interface Calling

Return instructions:


Under normal circumstances, WeChat will return the following JSON data packet to the public account:

{"access_token":"ACCESS_TOKEN", "expires_in":7200}


When an error occurs, WeChat will return error code information. An example of a JSON data packet is as follows (this example is an invalid AppID error) :

{"errcode":40013,"errmsg":"invalid appid"}

Note: When using access_token, please note that this interface has a calling frequency limit. The WeChat server will impose interface restrictions on the current official account based on the daily maximum call frequency. For specific details, please read the official document (WeChat official account interface frequency limit description)

More C# WeChat development series - Obtain interface call credentials related Please pay attention to the PHP Chinese website for articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template