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   * 微信企业号应用提供商凭证创建
12   *
13   * @className WeixinProviderTokenCreator
14   * @author jinyu(foxinmy@gmail.com)
15   * @date 2015年1月10日
16   * @since JDK 1.6
17   * @see <a href= "http://work.weixin.qq.com/api/doc#11791/服务商的凭证">服务商的凭证</a>
18   * @see com.foxinmy.weixin4j.model.Token
19   */
20  public class WeixinProviderTokenCreator extends TokenCreator {
21  
22  	private final String corpid;
23  	private final String providersecret;
24  
25  	/**
26  	 *
27  	 * @param corpid
28  	 *            企业号ID
29  	 * @param providersecret
30  	 *            企业号提供商的secret
31  	 */
32  	public WeixinProviderTokenCreator(String corpid, String providersecret) {
33  		this.corpid = corpid;
34  		this.providersecret = providersecret;
35  	}
36  
37  	@Override
38  	public String name() {
39  		return "qy_provider_token";
40  	}
41  
42  	@Override
43  	public String uniqueid() {
44  		return corpid;
45  	}
46  
47  	@Override
48  	public Token create() throws WeixinException {
49  		JSONObject obj = new JSONObject();
50  		obj.put("corpid", corpid);
51  		obj.put("provider_secret", providersecret);
52  		WeixinResponse response = weixinExecutor.post(
53  				URLConsts.PROVIDER_TOKEN_URL, obj.toJSONString());
54  		obj = response.getAsJson();
55  		return new Token(obj.getString("provider_access_token"),
56  				obj.getLongValue("expires_in") * 1000l);
57  	}
58  }