Skip to content

开放平台componentAccessToken 增加过期自动刷新 #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface WxOpenConfigStorage {

boolean isComponentAccessTokenExpired();

void expireComponentAccessToken();

void updateComponentAccessTokent(WxOpenComponentAccessToken componentAccessToken);

WxMpConfigStorage getWxMpConfigStorage(String appId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.google.gson.JsonObject;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.URIUtil;
Expand Down Expand Up @@ -112,13 +113,55 @@ public String getComponentAccessToken(boolean forceRefresh) throws WxErrorExcept
private String post(String uri, String postData) throws WxErrorException {
String componentAccessToken = getComponentAccessToken(false);
String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + "component_access_token=" + componentAccessToken;
return getWxOpenService().post(uriWithComponentAccessToken, postData);
try {
return getWxOpenService().post(uriWithComponentAccessToken, postData);
}catch (WxErrorException e){
WxError error = e.getError();
/*
* 发生以下情况时尝试刷新access_token
* 40001 获取access_token时AppSecret错误,或者access_token无效
* 42001 access_token超时
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口
*/
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) {
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
this.getWxOpenConfigStorage().expireComponentAccessToken();
if (this.getWxOpenConfigStorage().autoRefreshToken()) {
return this.post(uri, postData);
}
}
if (error.getErrorCode() != 0) {
throw new WxErrorException(error, e);
}
return null;
}
}

private String get(String uri) throws WxErrorException {
String componentAccessToken = getComponentAccessToken(false);
String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + "component_access_token=" + componentAccessToken;
return getWxOpenService().get(uriWithComponentAccessToken, null);
try {
return getWxOpenService().get(uriWithComponentAccessToken, null);
}catch (WxErrorException e){
WxError error = e.getError();
/*
* 发生以下情况时尝试刷新access_token
* 40001 获取access_token时AppSecret错误,或者access_token无效
* 42001 access_token超时
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口
*/
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) {
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
this.getWxOpenConfigStorage().expireComponentAccessToken();
if (this.getWxOpenConfigStorage().autoRefreshToken()) {
return this.get(uri);
}
}
if (error.getErrorCode() != 0) {
throw new WxErrorException(error, e);
}
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public boolean isComponentAccessTokenExpired() {
return System.currentTimeMillis() > componentExpiresTime;
}

@Override
public void expireComponentAccessToken() {
this.componentExpiresTime = 0L;
}

@Override
public void updateComponentAccessTokent(WxOpenComponentAccessToken componentAccessToken) {
updateComponentAccessTokent(componentAccessToken.getComponentAccessToken(), componentAccessToken.getExpiresIn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public boolean isComponentAccessTokenExpired() {
}
}

@Override
public void expireComponentAccessToken(){
try (Jedis jedis = this.jedisPool.getResource()) {
jedis.expire(this.componentAccessTokenKey, 0);
}
}

@Override
public void updateComponentAccessTokent(String componentAccessToken, int expiresInSeconds) {
try (Jedis jedis = this.jedisPool.getResource()) {
Expand Down