Token优化
This commit is contained in:
+56
-60
@@ -1,60 +1,56 @@
|
||||
package com.foxinmy.weixin4j.qy.suite;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
|
||||
/**
|
||||
* 微信企业号应用套件预授权码创建
|
||||
*
|
||||
* @className WeixinSuitePreCodeCreator
|
||||
* @author jy
|
||||
* @date 2015年6月17日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E9.A2.84.E6.8E.88.E6.9D.83.E7.A0.81">获取应用套件预授权码</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinSuitePreCodeCreator implements TokenCreator {
|
||||
|
||||
private final WeixinRequestExecutor weixinExecutor;
|
||||
private final TokenHolder suiteTokenHolder;
|
||||
private final String suiteId;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param suiteTokenHolder
|
||||
* 应用套件的token
|
||||
* @param suiteId
|
||||
* 应用套件ID
|
||||
*/
|
||||
public WeixinSuitePreCodeCreator(TokenHolder suiteTokenHolder,
|
||||
String suiteId) {
|
||||
this.suiteTokenHolder = suiteTokenHolder;
|
||||
this.suiteId = suiteId;
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("weixin4j_qy_suite_precode_%s", suiteId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.SUITE_PRE_CODE_URL,
|
||||
suiteTokenHolder.getAccessToken()),
|
||||
String.format("{\"suite_id\":\"%s\"}", suiteId));
|
||||
JSONObject result = response.getAsJson();
|
||||
Token token = new Token(result.getString("pre_auth_code"));
|
||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
package com.foxinmy.weixin4j.qy.suite;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
|
||||
/**
|
||||
* 微信企业号应用套件预授权码创建
|
||||
*
|
||||
* @className WeixinSuitePreCodeCreator
|
||||
* @author jy
|
||||
* @date 2015年6月17日
|
||||
* @since JDK 1.6
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E9.A2.84.E6.8E.88.E6.9D.83.E7.A0.81">
|
||||
* 获取应用套件预授权码</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinSuitePreCodeCreator extends AbstractTokenCreator {
|
||||
|
||||
private final TokenHolder suiteTokenHolder;
|
||||
private final String suiteId;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param suiteTokenHolder
|
||||
* 应用套件的token
|
||||
* @param suiteId
|
||||
* 应用套件ID
|
||||
*/
|
||||
public WeixinSuitePreCodeCreator(TokenHolder suiteTokenHolder, String suiteId) {
|
||||
this.suiteTokenHolder = suiteTokenHolder;
|
||||
this.suiteId = suiteId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
return String.format("qy_suite_precode_%s", suiteId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.SUITE_PRE_CODE_URL, suiteTokenHolder.getAccessToken()),
|
||||
String.format("{\"suite_id\":\"%s\"}", suiteId));
|
||||
JSONObject result = response.getAsJson();
|
||||
Token token = new Token(result.getString("pre_auth_code"));
|
||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
+53
-56
@@ -1,56 +1,53 @@
|
||||
package com.foxinmy.weixin4j.qy.suite;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号应用套件凭证创建
|
||||
*
|
||||
* @className WeixinSuiteTokenCreator
|
||||
* @author jy
|
||||
* @date 2015年6月17日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinSuiteTokenCreator implements TokenCreator {
|
||||
|
||||
private final WeixinRequestExecutor weixinExecutor;
|
||||
private final SuiteTicketHolder ticketHolder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stringStorager
|
||||
* 套件ticket存取器
|
||||
*/
|
||||
public WeixinSuiteTokenCreator(SuiteTicketHolder ticketHolder) {
|
||||
this.ticketHolder = ticketHolder;
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("weixin4j_qy_suite_token_%s", ticketHolder.getSuiteId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", ticketHolder.getSuiteId());
|
||||
obj.put("suite_secret", ticketHolder.getSuiteSecret());
|
||||
obj.put("suite_ticket", ticketHolder.getTicket());
|
||||
WeixinResponse response = weixinExecutor.post(URLConsts.SUITE_TOKEN_URL,
|
||||
obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
Token token = new Token(obj.getString("suite_access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
package com.foxinmy.weixin4j.qy.suite;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号应用套件凭证创建
|
||||
*
|
||||
* @className WeixinSuiteTokenCreator
|
||||
* @author jy
|
||||
* @date 2015年6月17日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinSuiteTokenCreator extends AbstractTokenCreator {
|
||||
|
||||
private final SuiteTicketHolder ticketHolder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stringStorager
|
||||
* 套件ticket存取器
|
||||
*/
|
||||
public WeixinSuiteTokenCreator(SuiteTicketHolder ticketHolder) {
|
||||
this.ticketHolder = ticketHolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
return String.format("qy_suite_token_%s", ticketHolder.getSuiteId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", ticketHolder.getSuiteId());
|
||||
obj.put("suite_secret", ticketHolder.getSuiteSecret());
|
||||
obj.put("suite_ticket", ticketHolder.getTicket());
|
||||
WeixinResponse response = weixinExecutor.post(URLConsts.SUITE_TOKEN_URL,
|
||||
obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
Token token = new Token(obj.getString("suite_access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
+10
-17
@@ -2,11 +2,10 @@ package com.foxinmy.weixin4j.qy.suite;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
|
||||
/**
|
||||
@@ -16,13 +15,13 @@ import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
* @author jy
|
||||
* @date 2015年6月17日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7access_token">获取企业号access_token</a>
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7access_token">
|
||||
* 获取企业号access_token</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenSuiteCreator implements TokenCreator {
|
||||
public class WeixinTokenSuiteCreator extends AbstractTokenCreator {
|
||||
|
||||
private final WeixinRequestExecutor weixinExecutor;
|
||||
private final SuitePerCodeHolder perCodeHolder;
|
||||
private final TokenHolder suiteTokenHolder;
|
||||
|
||||
@@ -33,19 +32,14 @@ public class WeixinTokenSuiteCreator implements TokenCreator {
|
||||
* @param suiteTokenHolder
|
||||
* 第三方套件凭证token
|
||||
*/
|
||||
public WeixinTokenSuiteCreator(SuitePerCodeHolder perCodeHolder,
|
||||
TokenHolder suiteTokenHolder) {
|
||||
public WeixinTokenSuiteCreator(SuitePerCodeHolder perCodeHolder, TokenHolder suiteTokenHolder) {
|
||||
this.perCodeHolder = perCodeHolder;
|
||||
this.suiteTokenHolder = suiteTokenHolder;
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("weixin4j_qy_token_suite_%s_%s",
|
||||
perCodeHolder.getSuiteId(), perCodeHolder.getAuthCorpId()
|
||||
|
||||
);
|
||||
public String getCacheKey0() {
|
||||
return String.format("qy_token_suite_%s_%s", perCodeHolder.getSuiteId(), perCodeHolder.getAuthCorpId());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -54,9 +48,8 @@ public class WeixinTokenSuiteCreator implements TokenCreator {
|
||||
obj.put("suite_id", perCodeHolder.getSuiteId());
|
||||
obj.put("auth_corpid", perCodeHolder.getAuthCorpId());
|
||||
obj.put("permanent_code", perCodeHolder.getPermanentCode());
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.TOKEN_SUITE_URL,
|
||||
suiteTokenHolder.getAccessToken()), obj.toJSONString());
|
||||
WeixinResponse response = weixinExecutor
|
||||
.post(String.format(URLConsts.TOKEN_SUITE_URL, suiteTokenHolder.getAccessToken()), obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
Token token = new Token(obj.getString("access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
|
||||
+56
-59
@@ -1,59 +1,56 @@
|
||||
package com.foxinmy.weixin4j.qy.token;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号应用提供商凭证创建
|
||||
*
|
||||
* @className WeixinTokenCreator
|
||||
* @author jy
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E5%BA%94%E7%94%A8%E6%8F%90%E4%BE%9B%E5%95%86%E5%87%AD%E8%AF%81">获取应用提供商凭证</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinProviderTokenCreator implements TokenCreator {
|
||||
|
||||
private final WeixinRequestExecutor weixinExecutor;
|
||||
private final String corpid;
|
||||
private final String providersecret;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param providersecret
|
||||
* 企业号提供商的secret
|
||||
*/
|
||||
public WeixinProviderTokenCreator(String corpid, String providersecret) {
|
||||
this.corpid = corpid;
|
||||
this.providersecret = providersecret;
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("weixin4j_qy_provider_token_%s", corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("corpid", corpid);
|
||||
obj.put("provider_secret", providersecret);
|
||||
WeixinResponse response = weixinExecutor.post(URLConsts.PROVIDER_TOKEN_URL,
|
||||
obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
Token token = new Token(obj.getString("provider_access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
package com.foxinmy.weixin4j.qy.token;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号应用提供商凭证创建
|
||||
*
|
||||
* @className WeixinTokenCreator
|
||||
* @author jy
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.6
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E5%BA%94%E7%94%A8%E6%8F%90%E4%BE%9B%E5%95%86%E5%87%AD%E8%AF%81">
|
||||
* 获取应用提供商凭证</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinProviderTokenCreator extends AbstractTokenCreator {
|
||||
|
||||
private final String corpid;
|
||||
private final String providersecret;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param providersecret
|
||||
* 企业号提供商的secret
|
||||
*/
|
||||
public WeixinProviderTokenCreator(String corpid, String providersecret) {
|
||||
this.corpid = corpid;
|
||||
this.providersecret = providersecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
return String.format("qy_provider_token_%s", corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("corpid", corpid);
|
||||
obj.put("provider_secret", providersecret);
|
||||
WeixinResponse response = weixinExecutor.post(URLConsts.PROVIDER_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
Token token = new Token(obj.getString("provider_access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,10 @@ package com.foxinmy.weixin4j.qy.token;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.type.TicketType;
|
||||
|
||||
@@ -20,12 +19,11 @@ import com.foxinmy.weixin4j.type.TicketType;
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95"
|
||||
* >JSTICKET</a>
|
||||
*/
|
||||
public class WeixinTicketCreator implements TokenCreator {
|
||||
public class WeixinTicketCreator extends AbstractTokenCreator {
|
||||
|
||||
private final String corpid;
|
||||
private final TicketType ticketType;
|
||||
private final TokenHolder weixinTokenHolder;
|
||||
private final WeixinRequestExecutor weixinExecutor;
|
||||
|
||||
/**
|
||||
* @param corpid
|
||||
@@ -39,12 +37,11 @@ public class WeixinTicketCreator implements TokenCreator {
|
||||
this.corpid = corpid;
|
||||
this.ticketType = ticketType;
|
||||
this.weixinTokenHolder = weixinTokenHolder;
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("weixin4j_qy_ticket_%s_%s", ticketType.name(), corpid);
|
||||
public String getCacheKey0() {
|
||||
return String.format("qy_ticket_%s_%s", ticketType.name(), corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,56 +1,56 @@
|
||||
package com.foxinmy.weixin4j.qy.token;
|
||||
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号TOKEN创建
|
||||
*
|
||||
* @className WeixinTokenCreator
|
||||
* @author jy
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenCreator implements TokenCreator {
|
||||
|
||||
private final WeixinRequestExecutor weixinExecutor;
|
||||
private final String corpid;
|
||||
private final String corpsecret;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param corpsecret
|
||||
* 企业号secret
|
||||
*/
|
||||
public WeixinTokenCreator(String corpid, String corpsecret) {
|
||||
this.corpid = corpid;
|
||||
this.corpsecret = corpsecret;
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("weixin4j_qy_token_%s", corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid,
|
||||
corpsecret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
Token token = response.getAsObject(new TypeReference<Token>() {
|
||||
});
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
package com.foxinmy.weixin4j.qy.token;
|
||||
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号TOKEN创建
|
||||
*
|
||||
* @className WeixinTokenCreator
|
||||
* @author jy
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.6
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">
|
||||
* 微信企业号获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenCreator extends AbstractTokenCreator {
|
||||
|
||||
private final WeixinRequestExecutor weixinExecutor;
|
||||
private final String corpid;
|
||||
private final String corpsecret;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param corpsecret
|
||||
* 企业号secret
|
||||
*/
|
||||
public WeixinTokenCreator(String corpid, String corpsecret) {
|
||||
this.corpid = corpid;
|
||||
this.corpsecret = corpsecret;
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
return String.format("qy_token_%s", corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid, corpsecret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
Token token = response.getAsObject(new TypeReference<Token>() {
|
||||
});
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user