C # WeChat Development Series (3)-getting interface call creden》,

Source: Internet
Author: User

C # Development Series (3)-getting interface call creden》,
3.0 obtain interface call CREDEN

 

① Interface Description

 

Access_token is the globally unique ticket of the public account. access_token is required when the Public Account calls each interface. Developers need to properly store them. The storage of access_token must contain at least 512 characters. The validity period of access_token is currently 2 h (7200 s). You need to refresh it regularly. Repeated access_token acquisition will invalidate the last access_token.

Description of how to use and generate access_token for API calls on the public platform:

1. To keep the appsecrect confidential, a third party needs an access key token to obtain and refresh the Central Control Server. The access_token used by other business logic servers comes from the Central Control Server and should not be refreshed separately. Otherwise, the access_token overwrites the service;

2. Currently, the validity period of the access_token is expressed by the returned expire_in. Currently, the value is within 7200 seconds. The central control server needs to refresh the new access_token in advance based on the validity period. During the refresh process, the Central Control Server still outputs the old access_token. At this time, the background of the public platform will ensure that the new and old access_token are available within a short period of time, this ensures the smooth transition of third-party services;

3. The validity period of access_token may be adjusted in the future. Therefore, the Central Control Server not only needs to regularly refresh internally, but also needs to provide an interface for passively refresh access_token, in this way, the Service server can trigger the refresh process of the access_token when the access_token has timed out during API calls.

How can I obtain AppID and AppSecret !?

The public account can use AppID and AppSecret to call this interface to obtain the access_token. AppID and AppSecret can be found on the official website of the public platform-Developer Center page. (You must be a developer and the account is not in an abnormal state)

Note:Https protocol is used when all interfaces are called. If a third party does not use the central control server but selects each business logic point to refresh the access_taken, conflicts may occur, the service is unstable.

 

② Request Interface

 

API 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:

 

The specific implementation code is as follows:

1 /// <summary> 2 /// get ACCESS_TOKEN 3 /// </summary> 4 /// <returns> return operation credential </returns> 5 public string GetAccessToken () 6 {7 if (HttpContext. current. cache ["access_token"] = null) 8 {9 string para = string. format ("grant_type = client_credential & appid = {0} & secret = {1}", AppID, AppSecret); 10 string results = SendHTTPRequest ("POST", "https://api.weixin.qq.com/cgi-bin/token ", para); 11 JObject obj = (JObject) JsonConvert. deserializeObject (results ); 12 13 // ******************************** set the access_token expiration Mechanism * ************************* 14 15 Cache = HttpContext. current. cache; 16 cache. insert ("access_token", obj ["access_token"]. toString (), null, DateTime. now. addSeconds (7000 ),
System. web. caching. cache. noSlidingExpiration ); 17 18 //************************************ ********************************* * *** 19 return HttpContext. current. cache ["access_token"]. toString (); 20} 21 else22 {23 return HttpContext. current. cache ["access_token"]. toString (); 24} 25}

Response parameters:

 

Return description:

Normally, the following JSON data packet is returned to the Public Account:

{& Quot; access_token & quot;: & quot; ACCESS_TOKEN & quot;, & quot; expires_in & quot;: 7200}

 

The error code is returned when an error occurs. The following is an example of a JSON data packet (this is an invalid AppID error ):

{& Quot; errcode & quot;: 40013, & quot; errmsg & quot;: & quot; invalid appid & quot "}

Note:When using access_token, note that this interface has a call frequency limit. When the maximum call frequency is exceeded, the server will limit the current public number, for details, please read the official documentation (frequency limit of public number interfaces)

 

If you are interested in the Series C # Development Series, you can follow my other articles (continuously updating:

C # Development Series (Top)-complete learning path for development

C # Development Series (3)-getting interface call creden》

C # development series (2)-custom menu management

C # Development Series (1)-enable developer Mode

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.