com.foxinmy.weixin4j.mp.token.WeixinTokenCreator Maven / Gradle / Ivy
package com.foxinmy.weixin4j.mp.token;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.WeixinHttpClient;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.mp.type.URLConsts;
import com.foxinmy.weixin4j.token.TokenCreator;
/**
* 微信公众平台TOKEN创建者
*
* @className WeixinTokenCreator
* @author jy
* @date 2015年1月10日
* @since JDK 1.7
* @see 微信公众平台获取token说明
* @see com.foxinmy.weixin4j.model.Token
*/
public class WeixinTokenCreator implements TokenCreator {
private final WeixinHttpClient httpClient;
private final String appid;
private final String secret;
/**
*
* @param appid
* 公众号ID
* @param secret
* 公众号secret
*/
public WeixinTokenCreator(String appid, String secret) {
this.appid = appid;
this.secret = secret;
this.httpClient = new WeixinHttpClient();
}
@Override
public String getCacheKey() {
return String.format("mp_token_%s", appid);
}
@Override
public Token createToken() throws WeixinException {
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid,
secret);
WeixinResponse response = httpClient.get(tokenUrl);
Token token = response.getAsObject(new TypeReference() {
});
token.setTime(System.currentTimeMillis());
return token;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy