Java wechat development API step 3 wechat obtains and saves interface call credentials

How wechat obtains and saves interface call vouchers is described below

1、 Description * Please refer to the first two articles for details* This paper is divided into three parts: interface call credential access_ The function of token and how to obtain the interface call credential access_ How does token implement the "central control server" mentioned in wechat documents to save access_ Token * at the end of this article, all demonstration source codes including the first three articles will be given

Why should I get and save the interface call credentials access_ Token • start development - obtain interface call credentials ◦ document address: http://mp.weixin.qq.com/wiki/14/9f9c82c1af308e3b14ba9b973f99a8ba.html

• the official website document gives this explanation: · access_ Token is the official account of the official account. When the public number calls the interfaces, it needs access_. token。 Developers need to keep it properly. access_ At least 512 character space should be reserved for token storage. access_ The validity period of the token is currently 2 hours. It needs to be refreshed regularly. Repeated acquisition will result in the access obtained last time_ The token is invalid.

• understanding: · we can find that many advanced functions, such as custom menu, material management, user management, account management and other advanced functions, have the parameter "access_token = token" in the request link, which is a global call parameter. The wechat background needs to determine the identity according to this parameter, To ensure the safety of our WeChat official account. To prevent the program error of official account from causing WeChat server load exception, by default, every official account can not exceed the limit of the interface. Here, WeChat limits 2000 times a day. Therefore, if we want to call this parameter frequently, we developers need to save each access manually_ The valid period of the token is 2 hours.

Get interface call credential access_ The official website document of token gives the following explanation: · description of interface call request

HTTP request method: get https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET ■ return instructions

Normally, WeChat will return the following JSON packets to the official account: {"access_token": "ACCESS_TOKEN", "expires_in": 7200})

In case of error, wechat will return error code and other information. An example of JSON packet is as follows (this example is an invalid appid error): {"errCode": 40013, "errmsg": "invalid appid"}

• understand: · get request, which can be implemented directly locally. Because it is just an ordinary get request, which is similar to accessing a web address. Therefore, you can operate directly without uploading this part of the code to the server. · the HTTP request protocol is a get request, which indicates that we need to obtain the return flow through the get request, and the return flow is in JSON form. When calling, we need to carry three parameters: Grant_ type、appid、secret。 Appid and secret are the key parameters of our WeChat official account. The returned results are divided into correct and wrong results. [Baidu: JSON] · in fact, we can directly enter the document in the address bar to give an example: https://api.weixin.qq.com/cgi-bin/token ? grant_ type=client_ Credential & appid = appid & Secret = appsecret, we will see the following information: "{" errCode ": 40013," errmsg ":" invalid appid hint: [pqkl0120ic11]} ", because this is an invalid request and returns an error result. · when we replace the above two parameters with our test numbers appid and appsecret, we will see such information: "{access_token": "xrllr3fnf... Badamio", "expires_in": 7200} ", which means success. · now let's get the return stream through Java code and get access_ token。

• implementation

Save interface call voucher access_ Token • ideas

Access to be obtained_ The token and the current time are stored in the file. When taking out, judge the time difference between the current time and the time recorded in the storage. If it is greater than max_ Time, re acquire, and store the acquired in file to replace the original content. If it is less than max_ Time, get directly.

• implementation

The first three articles demonstrate the source code: http://xiazai.jb51.net/201606/yuanma/WeixinApiDemo (jb51.net). rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>