AuthFacebookRequest.java 3.2 KB
Newer Older
1 2 3
package me.zhyd.oauth.request;

import com.alibaba.fastjson.JSONObject;
4
import me.zhyd.oauth.cache.AuthStateCache;
5
import me.zhyd.oauth.config.AuthConfig;
6
import me.zhyd.oauth.config.AuthDefaultSource;
智布道's avatar
智布道 已提交
7
import me.zhyd.oauth.enums.AuthUserGender;
8
import me.zhyd.oauth.exception.AuthException;
9
import me.zhyd.oauth.model.AuthCallback;
10 11
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
不合群的混子's avatar
不合群的混子 已提交
12
import me.zhyd.oauth.utils.UrlBuilder;
13 14 15 16 17

/**
 * Facebook登录
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
智布道's avatar
智布道 已提交
18
 * @since 1.3.0
19
 */
智布道's avatar
智布道 已提交
20
public class AuthFacebookRequest extends AuthDefaultRequest {
21 22

    public AuthFacebookRequest(AuthConfig config) {
23
        super(config, AuthDefaultSource.FACEBOOK);
24 25
    }

26
    public AuthFacebookRequest(AuthConfig config, AuthStateCache authStateCache) {
27
        super(config, AuthDefaultSource.FACEBOOK, authStateCache);
28 29
    }

30
    @Override
31
    protected AuthToken getAccessToken(AuthCallback authCallback) {
32 33
        String response = doPostAuthorizationCode(authCallback.getCode());
        JSONObject accessTokenObject = JSONObject.parseObject(response);
智布道's avatar
智布道 已提交
34
        this.checkResponse(accessTokenObject);
35
        return AuthToken.builder()
不合群的混子's avatar
不合群的混子 已提交
36 37 38 39
            .accessToken(accessTokenObject.getString("access_token"))
            .expireIn(accessTokenObject.getIntValue("expires_in"))
            .tokenType(accessTokenObject.getString("token_type"))
            .build();
40 41 42 43
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
44
        String userInfo = doGetUserInfo(authToken);
45
        JSONObject object = JSONObject.parseObject(userInfo);
智布道's avatar
智布道 已提交
46
        this.checkResponse(object);
47
        return AuthUser.builder()
不合群的混子's avatar
不合群的混子 已提交
48 49 50
            .uuid(object.getString("id"))
            .username(object.getString("name"))
            .nickname(object.getString("name"))
智布道's avatar
智布道 已提交
51
            .avatar(getUserPicture(object))
不合群的混子's avatar
不合群的混子 已提交
52 53 54 55
            .location(object.getString("locale"))
            .email(object.getString("email"))
            .gender(AuthUserGender.getRealGender(object.getString("gender")))
            .token(authToken)
56
            .source(source.toString())
不合群的混子's avatar
不合群的混子 已提交
57 58 59
            .build();
    }

智布道's avatar
智布道 已提交
60 61 62 63 64 65 66 67 68 69 70 71
    private String getUserPicture(JSONObject object) {
        String picture = null;
        if (object.containsKey("picture")) {
            JSONObject pictureObj = object.getJSONObject("picture");
            pictureObj = pictureObj.getJSONObject("data");
            if (null != pictureObj) {
                picture = pictureObj.getString("url");
            }
        }
        return picture;
    }

不合群的混子's avatar
不合群的混子 已提交
72 73 74
    /**
     * 返回获取userInfo的url
     *
智布道's avatar
智布道 已提交
75
     * @param authToken 用户token
不合群的混子's avatar
不合群的混子 已提交
76 77 78 79 80 81 82 83
     * @return 返回获取userInfo的url
     */
    @Override
    protected String userInfoUrl(AuthToken authToken) {
        return UrlBuilder.fromBaseUrl(source.userInfo())
            .queryParam("access_token", authToken.getAccessToken())
            .queryParam("fields", "id,name,birthday,gender,hometown,email,devices,picture.width(400)")
            .build();
84
    }
智布道's avatar
智布道 已提交
85 86 87 88 89 90 91 92 93 94 95

    /**
     * 检查响应内容是否正确
     *
     * @param object 请求响应内容
     */
    private void checkResponse(JSONObject object) {
        if (object.containsKey("error")) {
            throw new AuthException(object.getJSONObject("error").getString("message"));
        }
    }
96
}