mirror of
https://gitee.com/yadong.zhang/JustAuth.git
synced 2026-01-07 19:31:48 +08:00
78 lines
2.9 KiB
Java
78 lines
2.9 KiB
Java
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.config.AuthSource;
|
|
import me.zhyd.oauth.exception.AuthException;
|
|
import me.zhyd.oauth.model.AuthCallback;
|
|
import me.zhyd.oauth.model.AuthToken;
|
|
import me.zhyd.oauth.model.AuthUser;
|
|
import me.zhyd.oauth.model.AuthUserGender;
|
|
import me.zhyd.oauth.url.AuthFacebookUrlBuilder;
|
|
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
|
|
|
|
/**
|
|
* Facebook登录
|
|
*
|
|
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
|
* @version 1.0
|
|
* @since 1.8
|
|
*/
|
|
public class AuthFacebookRequest extends AuthDefaultRequest {
|
|
|
|
public AuthFacebookRequest(AuthConfig config) {
|
|
super(config, AuthSource.FACEBOOK, new AuthFacebookUrlBuilder());
|
|
}
|
|
|
|
@Override
|
|
protected AuthToken getAccessToken(AuthCallback authCallback) {
|
|
String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(authCallback.getCode());
|
|
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
|
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
|
|
|
if (accessTokenObject.containsKey("error")) {
|
|
throw new AuthException(accessTokenObject.getJSONObject("error").getString("message"));
|
|
}
|
|
|
|
return AuthToken.builder()
|
|
.accessToken(accessTokenObject.getString("access_token"))
|
|
.expireIn(accessTokenObject.getIntValue("expires_in"))
|
|
.tokenType(accessTokenObject.getString("token_type"))
|
|
.build();
|
|
}
|
|
|
|
@Override
|
|
protected AuthUser getUserInfo(AuthToken authToken) {
|
|
String accessToken = authToken.getAccessToken();
|
|
HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
|
|
.accessToken(accessToken)
|
|
.build())).execute();
|
|
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()
|
|
.uuid(object.getString("id"))
|
|
.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();
|
|
}
|
|
}
|