View Javadoc
1   package com.foxinmy.weixin4j.qy.suite;
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.qy.type.URLConsts;
8   import com.foxinmy.weixin4j.token.TicketManager;
9   import com.foxinmy.weixin4j.token.TokenCreator;
10  
11  /**
12   * 微信企业号应用套件凭证创建
13   *
14   * @className WeixinSuiteTokenCreator
15   * @author jinyu(foxinmy@gmail.com)
16   * @date 2015年6月17日
17   * @since JDK 1.6
18   * @see <a href= "http://work.weixin.qq.com/api/doc#10975/获取应用套件凭证">获取应用套件凭证</a>
19   * @see com.foxinmy.weixin4j.model.Token
20   */
21  public class WeixinSuiteTokenCreator extends TokenCreator {
22  
23  	private final TicketManager ticketManager;
24  
25  	/**
26  	 *
27  	 * @param ticketManager
28  	 *            套件ticket存取
29  	 */
30  	public WeixinSuiteTokenCreator(TicketManager ticketManager) {
31  		this.ticketManager = ticketManager;
32  	}
33  
34  	@Override
35  	public String name() {
36  		return "qy_suite_token";
37  	}
38  
39  	@Override
40  	public String uniqueid() {
41  		return ticketManager.getThirdId();
42  	}
43  
44  	@Override
45  	public Token create() throws WeixinException {
46  		JSONObject obj = new JSONObject();
47  		obj.put("suite_id", ticketManager.getThirdId());
48  		obj.put("suite_secret", ticketManager.getThirdSecret());
49  		obj.put("suite_ticket", ticketManager.getAccessTicket());
50  		WeixinResponse response = weixinExecutor.post(
51  				URLConsts.SUITE_TOKEN_URL, obj.toJSONString());
52  		obj = response.getAsJson();
53  		return new Token(obj.getString("suite_access_token"),
54  				obj.getLongValue("expires_in") * 1000l);
55  	}
56  }