AuthTencentCloudRequest.java 2.5 KB
Newer Older
1 2 3 4 5 6 7 8
package me.zhyd.oauth.request;

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

/**
15 16
 * 腾讯云登录
 *
17 18 19 20 21 22 23 24 25 26 27
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @since 1.8
 */
public class AuthTencentCloudRequest extends BaseAuthRequest {

    public AuthTencentCloudRequest(AuthConfig config) {
        super(config, AuthSource.TENCEN_CLOUD);
    }

    @Override
28
    protected AuthToken getAccessToken(String code) {
29 30 31 32 33 34
        String accessTokenUrl = UrlBuilder.getTencentCloudAccessTokenUrl(config.getClientId(), config.getClientSecret(), code);
        HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
        JSONObject object = JSONObject.parseObject(response.body());
        if (object.getIntValue("code") != 0) {
            throw new AuthException("Unable to get token from tencent cloud using code [" + code + "]: " + object.get("msg"));
        }
35 36 37
        return AuthToken.builder()
                .accessToken(object.getString("access_token"))
                .build();
38 39 40
    }

    @Override
41 42
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        HttpResponse response = HttpRequest.get(UrlBuilder.getTencentCloudUserInfoUrl(accessToken)).execute();
        JSONObject object = JSONObject.parseObject(response.body());
        if (object.getIntValue("code") != 0) {
            throw new AuthException(object.getString("msg"));
        }
        object = object.getJSONObject("data");
        return AuthUser.builder()
                .username(object.getString("name"))
                .avatar("https://dev.tencent.com/" + object.getString("avatar"))
                .blog("https://dev.tencent.com/" + object.getString("path"))
                .nickname(object.getString("name"))
                .company(object.getString("company"))
                .location(object.getString("location"))
                .gender(AuthUserGender.getRealGender(object.getString("sex")))
                .email(object.getString("email"))
                .remark(object.getString("slogan"))
                .accessToken(accessToken)
                .source(AuthSource.TENCEN_CLOUD)
                .build();
    }
}