提交 9aa693d2 编写于 作者: H Hongwei Peng

add refresh token method

上级 a6f25ec3
...@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject; ...@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender; import me.zhyd.oauth.model.AuthUserGender;
...@@ -16,6 +17,7 @@ import me.zhyd.oauth.url.entity.AuthUserInfoEntity; ...@@ -16,6 +17,7 @@ import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
import java.util.Objects; import java.util.Objects;
import static me.zhyd.oauth.config.AuthSource.RENREN; 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 { ...@@ -32,26 +34,14 @@ public class AuthRenrenRequest extends AuthDefaultRequest {
@Override @Override
protected AuthToken getAccessToken(AuthCallback authCallback) { protected AuthToken getAccessToken(AuthCallback authCallback) {
String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(authCallback.getCode()); return getToken(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();
} }
@Override @Override
protected AuthUser getUserInfo(AuthToken authToken) { protected AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
.openId(authToken.getOpenId()) .openId(authToken.getOpenId())
.accessToken(accessToken) .accessToken(authToken.getAccessToken())
.build())).execute(); .build())).execute();
JSONObject userObj = JSONObject.parseObject(response.body()).getJSONObject("response"); JSONObject userObj = JSONObject.parseObject(response.body()).getJSONObject("response");
...@@ -66,6 +56,30 @@ public class AuthRenrenRequest extends AuthDefaultRequest { ...@@ -66,6 +56,30 @@ public class AuthRenrenRequest extends AuthDefaultRequest {
.build(); .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) { private String getAvatarUrl(JSONObject userObj) {
JSONArray jsonArray = userObj.getJSONArray("avatar"); JSONArray jsonArray = userObj.getJSONArray("avatar");
if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) { if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册