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
14
15
16
17
18
19
20 public class WeixinProviderTokenCreator extends TokenCreator {
21
22 private final String corpid;
23 private final String providersecret;
24
25
26
27
28
29
30
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 }