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.AuthToken;
11 12
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender;
13
import me.zhyd.oauth.utils.GlobalAuthUtil;
14 15 16
import me.zhyd.oauth.utils.StringUtils;
import me.zhyd.oauth.utils.UrlBuilder;

17 18
import java.util.Map;

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

    @Override
33
    protected AuthToken getAccessToken(String code) {
34 35 36 37
        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());
38 39 40
        if (!accessTokenObject.containsKey("access_token")) {
            throw new AuthException("Unable to get token from qq using code [" + code + "]");
        }
41
        return AuthToken.builder()
42 43 44
                .accessToken(accessTokenObject.get("access_token"))
                .expireIn(Integer.valueOf(accessTokenObject.get("expires_in")))
                .refreshToken(accessTokenObject.get("refresh_token"))
45
                .build();
46 47 48
    }

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

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

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

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

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