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
import me.zhyd.oauth.utils.GlobalAuthUtil;
15 16 17
import me.zhyd.oauth.utils.StringUtils;
import me.zhyd.oauth.utils.UrlBuilder;

18 19
import java.util.Map;

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

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

    @Override
50 51
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
智布道's avatar
智布道 已提交
52
        String openId = this.getOpenId(authToken);
53 54
        HttpResponse response = HttpRequest.get(UrlBuilder.getQqUserInfoUrl(config.getClientId(), accessToken, openId))
                .execute();
55 56 57 58 59 60 61 62
        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
智布道 已提交
63 64

        String location = String.format("%s-%s", object.getString("province"), object.getString("city"));
65 66 67 68
        return AuthUser.builder()
                .username(object.getString("nickname"))
                .nickname(object.getString("nickname"))
                .avatar(avatar)
智布道's avatar
智布道 已提交
69
                .location(location)
70
                .uuid(openId)
71
                .gender(AuthUserGender.getRealGender(object.getString("gender")))
72
                .token(authToken)
73 74 75 76
                .source(AuthSource.QQ)
                .build();
    }

77 78 79 80 81 82 83 84 85 86
    /**
     * 返回认证url,可自行跳转页面
     *
     * @return 返回授权地址
     */
    @Override
    public String authorize() {
        return UrlBuilder.getQqAuthorizeUrl(config.getClientId(), config.getRedirectUri());
    }

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

        throw new AuthException("request error");
108 109
    }
}