From 9aa693d2da6a19f52980d58afbd7c8afa033eee9 Mon Sep 17 00:00:00 2001 From: Hongwei Peng Date: Thu, 18 Jul 2019 21:06:03 +0800 Subject: [PATCH] add refresh token method --- .../zhyd/oauth/request/AuthRenrenRequest.java | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/java/me/zhyd/oauth/request/AuthRenrenRequest.java b/src/main/java/me/zhyd/oauth/request/AuthRenrenRequest.java index dc74e89..8468958 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthRenrenRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthRenrenRequest.java @@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.model.AuthResponse; import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUserGender; @@ -16,6 +17,7 @@ import me.zhyd.oauth.url.entity.AuthUserInfoEntity; import java.util.Objects; import static me.zhyd.oauth.config.AuthSource.RENREN; +import static me.zhyd.oauth.model.AuthResponseStatus.SUCCESS; /** * 人人登录 @@ -32,26 +34,14 @@ public class AuthRenrenRequest extends AuthDefaultRequest { @Override protected AuthToken getAccessToken(AuthCallback authCallback) { - String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(authCallback.getCode()); - HttpResponse response = HttpRequest.post(accessTokenUrl).execute(); - JSONObject accessTokenObject = JSONObject.parseObject(response.body()); - if (!response.isOk()) { - throw new AuthException("Unable to get token from renren using code [" + authCallback.getCode() + "]: " + accessTokenObject); - } - - return AuthToken.builder() - .accessToken(accessTokenObject.getString("access_token")) - .refreshToken(accessTokenObject.getString("refresh_token")) - .openId(accessTokenObject.getJSONObject("user").getString("id")) - .build(); + return getToken(this.urlBuilder.getAccessTokenUrl(authCallback.getCode())); } @Override protected AuthUser getUserInfo(AuthToken authToken) { - String accessToken = authToken.getAccessToken(); HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() .openId(authToken.getOpenId()) - .accessToken(accessToken) + .accessToken(authToken.getAccessToken()) .build())).execute(); JSONObject userObj = JSONObject.parseObject(response.body()).getJSONObject("response"); @@ -66,6 +56,30 @@ public class AuthRenrenRequest extends AuthDefaultRequest { .build(); } + @Override + public AuthResponse refresh(AuthToken authToken) { + return AuthResponse.builder() + .code(SUCCESS.getCode()) + .data(getToken(this.urlBuilder.getRefreshUrl(authToken.getRefreshToken()))) + .build(); + } + + private AuthToken getToken(String url) { + HttpResponse response = HttpRequest.post(url).execute(); + JSONObject jsonObject = JSONObject.parseObject(response.body()); + if (!response.isOk()) { + throw new AuthException("Failed to get token from Renren: " + jsonObject); + } + + return AuthToken.builder() + .tokenType(jsonObject.getString("token_type")) + .expireIn(jsonObject.getIntValue("expires_in")) + .accessToken(jsonObject.getString("access_token")) + .refreshToken(jsonObject.getString("refresh_token")) + .openId(jsonObject.getJSONObject("user").getString("id")) + .build(); + } + private String getAvatarUrl(JSONObject userObj) { JSONArray jsonArray = userObj.getJSONArray("avatar"); if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) { -- GitLab