AuthFacebookRequest.java 3.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 15 16
import me.zhyd.oauth.url.FacebookUrlBuilder;
import me.zhyd.oauth.url.entity.AuthAccessTokenEntity;
import me.zhyd.oauth.url.entity.AuthAuthorizeEntity;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
17 18 19 20 21 22 23 24 25 26 27

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

    public AuthFacebookRequest(AuthConfig config) {
28
        super(config, AuthSource.FACEBOOK, new FacebookUrlBuilder());
29 30 31
    }

    @Override
32
    protected AuthToken getAccessToken(AuthCallback authCallback) {
33 34 35 36
        String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder()
                .config(config)
                .code(authCallback.getCode())
                .build());
37
        HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
智布道's avatar
智布道 已提交
38
        JSONObject accessTokenObject = JSONObject.parseObject(response.body());
39

智布道's avatar
智布道 已提交
40 41
        if (accessTokenObject.containsKey("error")) {
            throw new AuthException(accessTokenObject.getJSONObject("error").getString("message"));
42 43 44
        }

        return AuthToken.builder()
智布道's avatar
智布道 已提交
45 46 47
                .accessToken(accessTokenObject.getString("access_token"))
                .expireIn(accessTokenObject.getIntValue("expires_in"))
                .tokenType(accessTokenObject.getString("token_type"))
48 49 50 51 52 53
                .build();
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
54 55 56
        HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
                .accessToken(accessToken)
                .build())).execute();
57 58 59 60 61 62 63 64 65 66 67 68 69 70
        String userInfo = response.body();
        JSONObject object = JSONObject.parseObject(userInfo);
        if (object.containsKey("error")) {
            throw new AuthException(object.getJSONObject("error").getString("message"));
        }
        String picture = null;
        if (object.containsKey("picture")) {
            JSONObject pictureObj = object.getJSONObject("picture");
            pictureObj = pictureObj.getJSONObject("data");
            if (null != pictureObj) {
                picture = pictureObj.getString("url");
            }
        }
        return AuthUser.builder()
71
                .uuid(object.getString("id"))
72 73 74 75 76 77 78 79 80 81
                .username(object.getString("name"))
                .nickname(object.getString("name"))
                .avatar(picture)
                .location(object.getString("locale"))
                .email(object.getString("email"))
                .gender(AuthUserGender.getRealGender(object.getString("gender")))
                .token(authToken)
                .source(AuthSource.FACEBOOK)
                .build();
    }
82 83 84 85 86 87 88 89

    /**
     * 返回认证url,可自行跳转页面
     *
     * @return 返回授权地址
     */
    @Override
    public String authorize() {
90 91 92
        return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder()
                .config(config)
                .build());
93
    }
94
}