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 WeixinTokenCreator extends TokenCreator {
21
22 private final String corpid;
23 private final String corpsecret;
24
25
26
27
28
29
30
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 }