View Javadoc
1   package com.foxinmy.weixin4j.mp.component;
2   
3   import com.alibaba.fastjson.JSONObject;
4   import com.foxinmy.weixin4j.exception.WeixinException;
5   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
6   import com.foxinmy.weixin4j.model.Token;
7   import com.foxinmy.weixin4j.mp.type.URLConsts;
8   import com.foxinmy.weixin4j.token.TicketManager;
9   import com.foxinmy.weixin4j.token.TokenCreator;
10  
11  /**
12   * 微信开放平台应用组件凭证创建
13   *
14   * @className WeixinComponentTokenCreator
15   * @author jinyu(foxinmy@gmail.com)
16   * @date 2016年7月5日
17   * @since JDK 1.6
18   */
19  public class WeixinComponentTokenCreator extends TokenCreator {
20      private final TicketManager ticketManager;
21  
22      /**
23       *
24       * @param ticketManager
25       *            组件ticket存取
26       */
27      public WeixinComponentTokenCreator(TicketManager ticketManager) {
28          this.ticketManager = ticketManager;
29      }
30  
31      @Override
32      public String name() {
33          return "mp_component_token";
34      }
35  
36      @Override
37      public String uniqueid() {
38          return ticketManager.getThirdId();
39      }
40  
41      @Override
42      public Token create() throws WeixinException {
43          JSONObject obj = new JSONObject();
44          obj.put("component_appid", ticketManager.getThirdId());
45          obj.put("component_appsecret", ticketManager.getThirdSecret());
46          obj.put("component_verify_ticket", ticketManager.getAccessTicket());
47          WeixinResponse response = weixinExecutor.post(URLConsts.COMPONENT_TOKEN_URL, obj.toJSONString());
48          obj = response.getAsJson();
49          return new Token(obj.getString("component_access_token"), obj.getLongValue("expires_in") * 1000l);
50      }
51  }