AuthTaobaoRequest.java 2.4 KB
Newer Older
1 2 3 4 5 6
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;
7
import me.zhyd.oauth.config.AuthSource;
8
import me.zhyd.oauth.exception.AuthException;
9
import me.zhyd.oauth.model.AuthCallback;
10 11 12
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender;
13 14
import me.zhyd.oauth.url.TaobaoUrlBuilder;
import me.zhyd.oauth.url.entity.AuthAccessTokenEntity;
15 16 17 18 19 20 21 22 23 24 25 26
import me.zhyd.oauth.utils.GlobalAuthUtil;

/**
 * 淘宝登录
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @since 1.8
 */
public class AuthTaobaoRequest extends BaseAuthRequest {

    public AuthTaobaoRequest(AuthConfig config) {
27
        super(config, AuthSource.TAOBAO, new TaobaoUrlBuilder());
28 29 30
    }

    @Override
31 32
    protected AuthToken getAccessToken(AuthCallback authCallback) {
        return AuthToken.builder().accessCode(authCallback.getCode()).build();
33 34 35 36 37
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessCode = authToken.getAccessCode();
38 39 40 41
        HttpResponse response = HttpRequest.post(this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder()
                .config(config)
                .code(accessCode)
                .build())).execute();
智布道's avatar
智布道 已提交
42 43 44
        JSONObject accessTokenObject = JSONObject.parseObject(response.body());
        if (accessTokenObject.containsKey("error")) {
            throw new AuthException(ResponseStatus.FAILURE + ":" + accessTokenObject.getString("error_description"));
45
        }
智布道's avatar
智布道 已提交
46 47 48 49 50
        authToken.setAccessToken(accessTokenObject.getString("access_token"));
        authToken.setRefreshToken(accessTokenObject.getString("refresh_token"));
        authToken.setExpireIn(accessTokenObject.getIntValue("expires_in"));
        authToken.setUid(accessTokenObject.getString("taobao_user_id"));
        authToken.setOpenId(accessTokenObject.getString("taobao_open_uid"));
51

智布道's avatar
智布道 已提交
52
        String nick = GlobalAuthUtil.urlDecode(accessTokenObject.getString("taobao_user_nick"));
53
        return AuthUser.builder()
智布道's avatar
智布道 已提交
54
                .uuid(accessTokenObject.getString("taobao_user_id"))
55 56 57 58 59 60 61 62
                .username(nick)
                .nickname(nick)
                .gender(AuthUserGender.UNKNOW)
                .token(authToken)
                .source(AuthSource.TAOBAO)
                .build();
    }
}