WeChat Open Platform Development (3) Obtaining component_access_token

what is component_access_token

After getting the component_verify_ticket, we need to get a component_access_token, which is 第三方平台的下文中接口的调用凭据,也叫做令牌(component_access_token)。每个令牌是存在有效期(2小时)的,且令牌的调用不是无限制的。(the above is quoted from the official documents of the WeChat Open Platform) It should be noted that the access_token obtained on the WeChat public platform is limited to 2000 times per day, but the WeChat Open Platform only says that the call The number of times is limited, but it doesn't say what the upper limit is... Let's assume it's also the upper limit of 2000 times per day.

Each component_access_token is valid for 7200 seconds (2 hours).

How to get component_access_token

The interface to obtain component_access_token is very simple, just request https://api.weixin.qq.com/cgi-bin/component/api_component_token and bring the following three parameters (note that it is not a key-value transmission parameter):

  • component_appid, the appid of the open platform
  • component_appsecret, the appsecret of the open platform
  • component_verify_ticket, the component_verify_ticket pushed by the WeChat server every 10 minutes

Implementation plan

After obtaining the component_access_token, it can be used for 2 hours. The general strategy is to put it in redis. If there is no value next time, it will be pulled and stored in redis. The advantage of this is that any system connected to the redis can get the value, and this sample program is just to demonstrate the function, using a redis is a bit redundant. So I use the task debugger. The update strategy is to update every 2 minutes. If the last update time is 110 minutes ago or the value is empty, it will be pulled again, otherwise it will return directly.

It is relatively simple to use task debugging in Spring, just add a comment to a method of a bean (don't forget to scan this bean by Spring)

@Scheduled(cron = "0 0/2 * * * *")

The cron expression here means to start calling every 2 minutes, please check the cron expression yourself.

When using this function, be careful to add it in the Spring configuration file

<task:annotation-driven/>

That's it.

To facilitate testing, we add a method getComponentAccessToken() to AuthController to return the value of component_access_token on the page:

    /**
     * 获取Component_Access_Token
     */
    @RequestMapping("getComponentAccessToken")
    @ResponseBody
    public String getComponentAccessToken() {
        return componentAccessTokenTask.getComponentAccessToken();
    }

Similarly, it is also possible to add a method to actively refresh component_access_token, which is convenient for testing.

    /**
     * 刷新Component_Access_Token
     */
    @RequestMapping("refreshComponentAccessToken")
    @ResponseBody
    public String refreshComponentAccessToken() {
        componentAccessTokenTask.doComponentAccessTokenRefresh();
        return componentAccessTokenTask.getComponentAccessToken();
    }

It should be noted that in the production environment, this method must not be exposed or take certain measures to ensure that the number of calls does not reach the upper limit.

The code corresponding to this blog is at https://gitee.com/valuetodays/open-wx-demo/tree/V0.2

This blog was first published at http://eblog.doyourealizethattheimportantisdifficult.cn/article/getDetail.do?id=7

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325245083&siteId=291194637