AuthQqRequest.java 4.3 KB
Newer Older
1 2
package me.zhyd.oauth.request;

3
import cn.hutool.core.util.StrUtil;
4 5 6 7
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
8
import me.zhyd.oauth.config.AuthSource;
9
import me.zhyd.oauth.exception.AuthException;
10
import me.zhyd.oauth.model.AuthCallback;
11
import me.zhyd.oauth.model.AuthToken;
12 13
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender;
14 15 16
import me.zhyd.oauth.url.QqUrlBuilder;
import me.zhyd.oauth.url.entity.AuthAccessTokenEntity;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
17
import me.zhyd.oauth.utils.GlobalAuthUtil;
18 19
import me.zhyd.oauth.utils.StringUtils;

20 21
import java.util.Map;

22 23 24 25
/**
 * qq登录
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
26
 * @author yangkai.shen (https://xkcoding.com)
27 28 29 30 31
 * @version 1.0
 * @since 1.8
 */
public class AuthQqRequest extends BaseAuthRequest {
    public AuthQqRequest(AuthConfig config) {
32
        super(config, AuthSource.QQ, new QqUrlBuilder());
33 34 35
    }

    @Override
36
    protected AuthToken getAccessToken(AuthCallback authCallback) {
37 38 39 40
        String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder()
                .config(config)
                .code(authCallback.getCode())
                .build());
41 42
        HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
        Map<String, String> accessTokenObject = GlobalAuthUtil.parseStringToMap(response.body());
43
        if (!accessTokenObject.containsKey("access_token")) {
44
            throw new AuthException("Unable to get token from qq using code [" + authCallback.getCode() + "]");
45
        }
46
        return AuthToken.builder()
47 48 49
                .accessToken(accessTokenObject.get("access_token"))
                .expireIn(Integer.valueOf(accessTokenObject.get("expires_in")))
                .refreshToken(accessTokenObject.get("refresh_token"))
50
                .build();
51 52 53
    }

    @Override
54 55
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
智布道's avatar
智布道 已提交
56
        String openId = this.getOpenId(authToken);
57 58 59 60 61
        HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
                .clientId(config.getClientId())
                .accessToken(accessToken)
                .openId(openId)
                .build()))
62
                .execute();
63 64 65 66 67 68 69 70
        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");
        }
智布道's avatar
智布道 已提交
71 72

        String location = String.format("%s-%s", object.getString("province"), object.getString("city"));
73 74 75 76
        return AuthUser.builder()
                .username(object.getString("nickname"))
                .nickname(object.getString("nickname"))
                .avatar(avatar)
智布道's avatar
智布道 已提交
77
                .location(location)
78
                .uuid(openId)
79
                .gender(AuthUserGender.getRealGender(object.getString("gender")))
80
                .token(authToken)
81 82 83 84
                .source(AuthSource.QQ)
                .build();
    }

智布道's avatar
智布道 已提交
85 86
    private String getOpenId(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
87
        HttpResponse response = HttpRequest.get(this.urlBuilder.getOpenIdUrl(accessToken, config.isUnionId())).execute();
88
        if (response.isOk()) {
89 90 91 92 93
            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);
智布道's avatar
智布道 已提交
94 95
            if (object.containsKey("error")) {
                throw new AuthException(object.get("error") + ":" + object.get("error_description"));
96
            }
智布道's avatar
智布道 已提交
97
            authToken.setOpenId(object.getString("openid"));
98 99 100
            if (object.containsKey("unionid")) {
                authToken.setUnionId(object.getString("unionid"));
            }
101
            return StringUtils.isEmpty(authToken.getUnionId()) ? authToken.getOpenId() : authToken.getUnionId();
102
        }
S
skqing 已提交
103 104

        throw new AuthException("request error");
105 106
    }
}