提交 8d1f700e 编写于 作者: H Hongwei Peng

integrate Renren

上级 d977e05a
......@@ -444,6 +444,31 @@ public enum AuthSource {
public String userInfo() {
return "https://api.teambition.com/users/me";
}
},
/**
* 人人网
*/
RENREN {
@Override
public String authorize() {
return "https://graph.renren.com/oauth/authorize";
}
@Override
public String accessToken() {
return "https://graph.renren.com/oauth/token";
}
@Override
public String refresh() {
return "https://graph.renren.com/oauth/token";
}
@Override
public String userInfo() {
return "https://api.renren.com/v2/user/get";
}
};
/**
......@@ -485,4 +510,4 @@ public enum AuthSource {
throw new AuthException(AuthResponseStatus.UNSUPPORTED);
}
}
\ No newline at end of file
}
package me.zhyd.oauth.request;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
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.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender;
import me.zhyd.oauth.url.AuthRenrenUrlBuilder;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
import java.util.Objects;
import static me.zhyd.oauth.config.AuthSource.RENREN;
/**
* 人人登录
*
* @author hongwei.peng (pengisgood(at)gmail(dot)com)
* @version 1.8.1
* @since 1.8.1
*/
public class AuthRenrenRequest extends AuthDefaultRequest {
public AuthRenrenRequest(AuthConfig config) {
super(config, RENREN, new AuthRenrenUrlBuilder());
}
@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();
}
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
.openId(authToken.getOpenId())
.accessToken(accessToken)
.build())).execute();
JSONObject userObj = JSONObject.parseObject(response.body()).getJSONObject("response");
return AuthUser.builder()
.uuid(userObj.getString("id"))
.avatar(getAvatarUrl(userObj))
.nickname(userObj.getString("name"))
.company(getCompany(userObj))
.gender(getGender(userObj))
.token(authToken)
.source(RENREN)
.build();
}
private String getAvatarUrl(JSONObject userObj) {
JSONArray jsonArray = userObj.getJSONArray("avatar");
if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) {
return null;
}
return jsonArray.getJSONObject(0).getString("url");
}
private AuthUserGender getGender(JSONObject userObj) {
JSONObject basicInformation = userObj.getJSONObject("basicInformation");
if (Objects.isNull(basicInformation)) {
return AuthUserGender.UNKNOW;
}
return AuthUserGender.getRealGender(basicInformation.getString("sex"));
}
private String getCompany(JSONObject userObj) {
JSONArray jsonArray = userObj.getJSONArray("work");
if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) {
return null;
}
return jsonArray.getJSONObject(0).getString("name");
}
}
package me.zhyd.oauth.url;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthResponseStatus;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
import java.text.MessageFormat;
import static me.zhyd.oauth.config.AuthSource.RENREN;
/**
* 人人网相关的URL构建类
*
* @author hongwei.peng (pengisgood(at)gmail(dot)com)
* @version 1.8.1
* @since 1.8.1
*/
public class AuthRenrenUrlBuilder extends AuthDefaultUrlBuilder {
private static final String RENREN_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}";
private static final String RENREN_USER_INFO_PATTERN = "{0}?access_token={1}&userId={2}";
private static final String RENREN_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}";
private static final String RENREN_REFRESH_PATTERN = "{0}?refresh_token={1}&client_id={2}&client_secret={3}&grant_type=refresh_token";
@Override
public String getAccessTokenUrl(String code) {
return MessageFormat.format(RENREN_ACCESS_TOKEN_PATTERN, RENREN.accessToken(), config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
}
@Override
public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) {
return MessageFormat.format(RENREN_USER_INFO_PATTERN, RENREN.userInfo(), userInfoEntity.getAccessToken(), userInfoEntity.getOpenId());
}
@Override
public String getAuthorizeUrl() {
return MessageFormat.format(RENREN_AUTHORIZE_PATTERN, RENREN.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState()));
}
@Override
public String getRefreshUrl(String refreshToken) {
return MessageFormat.format(RENREN_REFRESH_PATTERN, RENREN.refresh(), refreshToken, config.getClientId(), config.getClientSecret());
}
@Override
public String getRevokeUrl(String accessToken) {
throw new AuthException(AuthResponseStatus.UNSUPPORTED);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册