2019-05-17 10:19:52 +08:00
|
|
|
package me.zhyd.oauth.request;
|
|
|
|
|
|
2019-05-22 19:05:13 +08:00
|
|
|
import cn.hutool.core.util.StrUtil;
|
2019-05-17 10:19:52 +08:00
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import me.zhyd.oauth.config.AuthConfig;
|
|
|
|
|
import me.zhyd.oauth.exception.AuthException;
|
|
|
|
|
import me.zhyd.oauth.model.AuthSource;
|
2019-05-17 18:37:57 +08:00
|
|
|
import me.zhyd.oauth.model.AuthToken;
|
2019-05-17 10:19:52 +08:00
|
|
|
import me.zhyd.oauth.model.AuthUser;
|
|
|
|
|
import me.zhyd.oauth.model.AuthUserGender;
|
2019-05-22 19:05:13 +08:00
|
|
|
import me.zhyd.oauth.utils.GlobalAuthUtil;
|
2019-05-17 10:19:52 +08:00
|
|
|
import me.zhyd.oauth.utils.StringUtils;
|
|
|
|
|
import me.zhyd.oauth.utils.UrlBuilder;
|
|
|
|
|
|
2019-05-22 19:05:13 +08:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
2019-05-17 10:19:52 +08:00
|
|
|
/**
|
|
|
|
|
* qq登录
|
|
|
|
|
*
|
|
|
|
|
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
2019-05-23 11:25:21 +08:00
|
|
|
* @author yangkai.shen (https://xkcoding.com)
|
2019-05-17 10:19:52 +08:00
|
|
|
* @version 1.0
|
|
|
|
|
* @since 1.8
|
|
|
|
|
*/
|
|
|
|
|
public class AuthQqRequest extends BaseAuthRequest {
|
|
|
|
|
public AuthQqRequest(AuthConfig config) {
|
|
|
|
|
super(config, AuthSource.QQ);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-17 18:37:57 +08:00
|
|
|
protected AuthToken getAccessToken(String code) {
|
2019-05-22 19:05:13 +08:00
|
|
|
String accessTokenUrl = UrlBuilder.getQqAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config
|
|
|
|
|
.getRedirectUri());
|
|
|
|
|
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
|
|
|
|
|
Map<String, String> accessTokenObject = GlobalAuthUtil.parseStringToMap(response.body());
|
2019-05-17 10:19:52 +08:00
|
|
|
if (!accessTokenObject.containsKey("access_token")) {
|
|
|
|
|
throw new AuthException("Unable to get token from qq using code [" + code + "]");
|
|
|
|
|
}
|
2019-05-17 18:37:57 +08:00
|
|
|
return AuthToken.builder()
|
2019-05-22 19:05:13 +08:00
|
|
|
.accessToken(accessTokenObject.get("access_token"))
|
|
|
|
|
.expireIn(Integer.valueOf(accessTokenObject.get("expires_in")))
|
|
|
|
|
.refreshToken(accessTokenObject.get("refresh_token"))
|
2019-05-17 18:37:57 +08:00
|
|
|
.build();
|
2019-05-17 10:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-17 18:37:57 +08:00
|
|
|
protected AuthUser getUserInfo(AuthToken authToken) {
|
|
|
|
|
String accessToken = authToken.getAccessToken();
|
2019-05-17 10:19:52 +08:00
|
|
|
String openId = this.getOpenId(accessToken);
|
2019-05-23 11:13:32 +08:00
|
|
|
HttpResponse response = HttpRequest.get(UrlBuilder.getQqUserInfoUrl(config.getClientId(), accessToken, openId))
|
|
|
|
|
.execute();
|
2019-05-17 10:19:52 +08:00
|
|
|
JSONObject object = JSONObject.parseObject(response.body());
|
|
|
|
|
if (object.getIntValue("ret") != 0) {
|
|
|
|
|
throw new AuthException(object.getString("msg"));
|
|
|
|
|
}
|
|
|
|
|
String avatar = object.getString("figureurl_qq_2");
|
|
|
|
|
if (StringUtils.isEmpty(avatar)) {
|
|
|
|
|
avatar = object.getString("figureurl_qq_1");
|
|
|
|
|
}
|
2019-06-18 19:27:11 +08:00
|
|
|
|
|
|
|
|
String location = String.format("%s-%s", object.getString("province"), object.getString("city"));
|
2019-05-17 10:19:52 +08:00
|
|
|
return AuthUser.builder()
|
|
|
|
|
.username(object.getString("nickname"))
|
|
|
|
|
.nickname(object.getString("nickname"))
|
|
|
|
|
.avatar(avatar)
|
2019-06-18 19:27:11 +08:00
|
|
|
.location(location)
|
2019-05-23 18:51:22 +08:00
|
|
|
.uuid(openId)
|
2019-05-17 10:19:52 +08:00
|
|
|
.gender(AuthUserGender.getRealGender(object.getString("gender")))
|
2019-05-18 15:31:17 +08:00
|
|
|
.token(authToken)
|
2019-05-17 10:19:52 +08:00
|
|
|
.source(AuthSource.QQ)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getOpenId(String accessToken) {
|
2019-05-22 19:05:13 +08:00
|
|
|
HttpResponse response = HttpRequest.get(UrlBuilder.getQqOpenidUrl("https://graph.qq.com/oauth2.0/me", accessToken))
|
|
|
|
|
.execute();
|
2019-05-17 10:19:52 +08:00
|
|
|
if (response.isOk()) {
|
2019-05-22 19:05:13 +08:00
|
|
|
String body = response.body();
|
|
|
|
|
String removePrefix = StrUtil.replace(body, "callback(", "");
|
|
|
|
|
String removeSuffix = StrUtil.replace(removePrefix, ");", "");
|
|
|
|
|
String openId = StrUtil.trim(removeSuffix);
|
|
|
|
|
JSONObject object = JSONObject.parseObject(openId);
|
2019-05-17 10:19:52 +08:00
|
|
|
if (object.containsKey("openid")) {
|
|
|
|
|
return object.getString("openid");
|
|
|
|
|
}
|
|
|
|
|
throw new AuthException("Invalid openId");
|
|
|
|
|
}
|
|
|
|
|
throw new AuthException("Invalid openId");
|
|
|
|
|
}
|
|
|
|
|
}
|