View Javadoc
1   package com.foxinmy.weixin4j.qy.token;
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.TokenCreator;
9   
10  /**
11   * 微信企业号TOKEN创建
12   *
13   * @className WeixinTokenCreator
14   * @author jinyu(foxinmy@gmail.com)
15   * @date 2015年1月10日
16   * @since JDK 1.6
17   * @see <a href= "https://work.weixin.qq.com/api/doc#10013"> 微信企业号获取token说明</a>
18   * @see com.foxinmy.weixin4j.model.Token
19   */
20  public class WeixinTokenCreator extends TokenCreator {
21  
22  	private final String corpid;
23  	private final String corpsecret;
24  
25  	/**
26  	 *
27  	 * @param corpid
28  	 *            企业号ID
29  	 * @param corpsecret
30  	 *            企业号secret
31  	 */
32  	public WeixinTokenCreator(String corpid, String corpsecret) {
33  		this.corpid = corpid;
34  		this.corpsecret = corpsecret;
35  	}
36  
37  	@Override
38  	public String name() {
39  		return "qy_token";
40  	}
41  
42  	@Override
43  	public String uniqueid() {
44  		return corpid;
45  	}
46  
47  	@Override
48  	public Token create() throws WeixinException {
49  		String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid,
50  				corpsecret);
51  		WeixinResponse response = weixinExecutor.get(tokenUrl);
52  		JSONObject result = response.getAsJson();
53  		return new Token(result.getString("access_token"),
54  				result.getLongValue("expires_in") * 1000l);
55  	}
56  }