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.PerTicketManager;
9   import com.foxinmy.weixin4j.token.TokenCreator;
10  import com.foxinmy.weixin4j.token.TokenManager;
11  
12  /**
13   * 微信企业号token创建(永久授权码)
14   *
15   * @className WeixinTokenSuiteCreator
16   * @author jinyu(foxinmy@gmail.com)
17   * @date 2015年6月17日
18   * @since JDK 1.6
19   * @see <a href= "http://work.weixin.qq.com/api/doc#10975/获取企业永久授权码">
20   *      获取企业号access_token</a>
21   * @see com.foxinmy.weixin4j.model.Token
22   */
23  public class WeixinTokenSuiteCreator extends TokenCreator {
24  
25  	private final PerTicketManager perTicketManager;
26  	private final TokenManager suiteTokenManager;
27  
28  	/**
29  	 *
30  	 * @param perTicketManager
31  	 *            第三方套件永久授权码
32  	 * @param suiteTokenManager
33  	 *            第三方套件凭证token
34  	 */
35  	public WeixinTokenSuiteCreator(PerTicketManager perTicketManager,
36  			TokenManager suiteTokenManager) {
37  		this.perTicketManager = perTicketManager;
38  		this.suiteTokenManager = suiteTokenManager;
39  	}
40  
41  	@Override
42  	public String name() {
43  		return String.format("qy_token_suite_%s_%s",
44  				perTicketManager.getThirdId(), perTicketManager.getAuthAppId());
45  	}
46  
47  	@Override
48  	public String uniqueid() {
49  		throw new UnsupportedOperationException();
50  	}
51  
52  	@Override
53  	public Token create() throws WeixinException {
54  		JSONObject obj = new JSONObject();
55  		obj.put("suite_id", perTicketManager.getThirdId());
56  		obj.put("auth_corpid", perTicketManager.getAuthAppId());
57  		obj.put("permanent_code", perTicketManager.getAccessTicket());
58  		WeixinResponse response = weixinExecutor
59  				.post(String.format(URLConsts.TOKEN_SUITE_URL,
60  						suiteTokenManager.getAccessToken()), obj.toJSONString());
61  		obj = response.getAsJson();
62  		return new Token(obj.getString("access_token"),
63  				obj.getLongValue("expires_in") * 1000l);
64  	}
65  }