提交 53df9924 编写于 作者: 智布道's avatar 智布道 👁

优化代码、修复google获取用户信息失败的问题

上级 e29df531
......@@ -294,7 +294,7 @@ public enum AuthSource {
@Override
public String userInfo() {
return "https://oauth2.googleapis.com/tokeninfo";
return "https://www.googleapis.com/oauth2/v3/userinfo";
}
},
/**
......
......@@ -81,7 +81,7 @@ public class AuthAlipayRequest extends AuthDefaultRequest {
.location(location)
.gender(AuthUserGender.getRealGender(response.getGender()))
.token(authToken)
.source(AuthSource.ALIPAY)
.source(source)
.build();
}
......
......@@ -8,6 +8,7 @@ import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;
import me.zhyd.oauth.utils.StringUtils;
import me.zhyd.oauth.utils.UrlBuilder;
/**
......@@ -39,14 +40,19 @@ public class AuthBaiduRequest extends AuthDefaultRequest {
.uuid(object.getString("userid"))
.username(object.getString("username"))
.nickname(object.getString("username"))
.avatar(object.getString("portrait"))
.avatar(getAvatar(object))
.remark(object.getString("userdetail"))
.gender(AuthUserGender.getRealGender(object.getString("sex")))
.token(authToken)
.source(AuthSource.BAIDU)
.source(source)
.build();
}
private String getAvatar(JSONObject object) {
String protrait = object.getString("portrait");
return StringUtils.isEmpty(protrait) ? null : String.format("http://himg.bdimg.com/sys/portrait/item/%s.jpg", protrait);
}
@Override
public AuthResponse revoke(AuthToken authToken) {
HttpResponse response = doGetRevoke(authToken);
......
......@@ -55,7 +55,7 @@ public class AuthCodingRequest extends AuthDefaultRequest {
.email(object.getString("email"))
.remark(object.getString("slogan"))
.token(authToken)
.source(AuthSource.CODING)
.source(source)
.build();
}
......
......@@ -44,7 +44,7 @@ public class AuthCsdnRequest extends AuthDefaultRequest {
.blog(object.getString("website"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.CSDN)
.source(source)
.build();
}
......
......@@ -20,7 +20,6 @@ import me.zhyd.oauth.utils.UrlBuilder;
* @version 1.0
* @since 1.8
*/
@Data
@Slf4j
public abstract class AuthDefaultRequest implements AuthRequest {
protected AuthConfig config;
......
......@@ -52,7 +52,7 @@ public class AuthDingTalkRequest extends AuthDefaultRequest {
.nickname(object.getString("nick"))
.username(object.getString("nick"))
.gender(AuthUserGender.UNKNOWN)
.source(AuthSource.DINGTALK)
.source(source)
.token(token)
.build();
}
......
......@@ -42,7 +42,7 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
.remark(userInfoObject.getString("description"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.DOUYIN)
.source(source)
.build();
}
......
......@@ -51,7 +51,7 @@ public class AuthFacebookRequest extends AuthDefaultRequest {
.email(object.getString("email"))
.gender(AuthUserGender.getRealGender(object.getString("gender")))
.token(authToken)
.source(AuthSource.FACEBOOK)
.source(source)
.build();
}
......
......@@ -55,7 +55,7 @@ public class AuthGiteeRequest extends AuthDefaultRequest {
.remark(object.getString("bio"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.GITEE)
.source(source)
.build();
}
......
......@@ -9,6 +9,9 @@ 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.utils.GlobalAuthUtil;
import java.util.Map;
/**
* Github登录
......@@ -26,12 +29,14 @@ public class AuthGithubRequest extends AuthDefaultRequest {
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
HttpResponse response = doPostAuthorizationCode(authCallback.getCode());
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
this.checkResponse(accessTokenObject);
Map<String, String> res = GlobalAuthUtil.parseStringToMap(response.body());
if (res.containsKey("error")) {
throw new AuthException(res.get("error") + ":" + res.get("error_description"));
}
return AuthToken.builder()
.accessToken(accessTokenObject.getString("access_token"))
.scope(accessTokenObject.getString("scope"))
.tokenType(accessTokenObject.getString("token_type"))
.accessToken(res.get("access_token"))
.scope(res.get("scope"))
.tokenType(res.get("token_type"))
.build();
}
......@@ -39,7 +44,9 @@ public class AuthGithubRequest extends AuthDefaultRequest {
protected AuthUser getUserInfo(AuthToken authToken) {
HttpResponse response = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(response.body());
this.checkResponse(object);
if (object.containsKey("error")) {
throw new AuthException(object.getString("error_description"));
}
return AuthUser.builder()
.uuid(object.getString("id"))
.username(object.getString("login"))
......@@ -52,7 +59,7 @@ public class AuthGithubRequest extends AuthDefaultRequest {
.remark(object.getString("bio"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.GITHUB)
.source(source)
.build();
}
......@@ -62,8 +69,6 @@ public class AuthGithubRequest extends AuthDefaultRequest {
* @param object 请求响应内容
*/
private void checkResponse(JSONObject object) {
if (object.containsKey("error")) {
throw new AuthException(object.getString("error_description"));
}
}
}
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;
......@@ -40,25 +41,28 @@ public class AuthGoogleRequest extends AuthDefaultRequest {
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
HttpResponse response = doGetUserInfo(authToken);
HttpResponse response = HttpRequest.post(userInfoUrl(authToken))
.header("Authorization", "Bearer " + authToken.getAccessToken())
.execute();
String userInfo = response.body();
JSONObject object = JSONObject.parseObject(userInfo);
this.checkResponse(object);
return AuthUser.builder()
.uuid(object.getString("sub"))
.username(object.getString("name"))
.username(object.getString("email"))
.avatar(object.getString("picture"))
.nickname(object.getString("name"))
.location(object.getString("locale"))
.email(object.getString("email"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.GOOGLE)
.source(source)
.build();
}
/**
* 返回认证url,可自行跳转页面
* https://openidconnect.googleapis.com/v1/userinfo
*
* @return 返回授权地址
*/
......@@ -81,7 +85,7 @@ public class AuthGoogleRequest extends AuthDefaultRequest {
*/
@Override
protected String userInfoUrl(AuthToken authToken) {
return UrlBuilder.fromBaseUrl(source.userInfo()).queryParam("id_token", authToken.getAccessToken()).build();
return UrlBuilder.fromBaseUrl(source.userInfo()).queryParam("access_token", authToken.getAccessToken()).build();
}
/**
......@@ -91,7 +95,7 @@ public class AuthGoogleRequest extends AuthDefaultRequest {
*/
private void checkResponse(JSONObject object) {
if (object.containsKey("error") || object.containsKey("error_description")) {
throw new AuthException(object.getString("error_description"));
throw new AuthException(object.containsKey("error") + ":" + object.getString("error_description"));
}
}
}
......@@ -75,7 +75,7 @@ public class AuthMiRequest extends AuthDefaultRequest {
.email(user.getString("mail"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.MI)
.source(source)
.build();
// 获取用户邮箱手机号等信息
......
......@@ -83,7 +83,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
.email(object.getString("mail"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.MICROSOFT)
.source(source)
.build();
}
......
......@@ -52,7 +52,7 @@ public class AuthOschinaRequest extends AuthDefaultRequest {
.gender(AuthUserGender.getRealGender(object.getString("gender")))
.email(object.getString("email"))
.token(authToken)
.source(AuthSource.OSCHINA)
.source(source)
.build();
}
......
......@@ -58,7 +58,7 @@ public class AuthPinterestRequest extends AuthDefaultRequest {
.gender(AuthUserGender.UNKNOWN)
.remark(userObj.getString("bio"))
.token(authToken)
.source(PINTEREST)
.source(source)
.build();
}
......
......@@ -65,7 +65,7 @@ public class AuthQqRequest extends AuthDefaultRequest {
.uuid(openId)
.gender(AuthUserGender.getRealGender(object.getString("gender")))
.token(authToken)
.source(AuthSource.QQ)
.source(source)
.build();
}
......
......@@ -45,7 +45,7 @@ public class AuthRenrenRequest extends AuthDefaultRequest {
.company(getCompany(userObj))
.gender(getGender(userObj))
.token(authToken)
.source(RENREN)
.source(source)
.build();
}
......
......@@ -63,7 +63,7 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
.blog(userObj.getString("website_url"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(STACK_OVERFLOW)
.source(source)
.build();
}
......
......@@ -50,7 +50,7 @@ public class AuthTaobaoRequest extends AuthDefaultRequest {
.nickname(nick)
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.TAOBAO)
.source(source)
.build();
}
......
......@@ -67,7 +67,7 @@ public class AuthTeambitionRequest extends AuthDefaultRequest {
.email(object.getString("email"))
.gender(AuthUserGender.UNKNOWN)
.token(authToken)
.source(AuthSource.TEAMBITION)
.source(source)
.build();
}
......
......@@ -55,7 +55,7 @@ public class AuthTencentCloudRequest extends AuthDefaultRequest {
.email(object.getString("email"))
.remark(object.getString("slogan"))
.token(authToken)
.source(AuthSource.TENCENT_CLOUD)
.source(source)
.build();
}
......
......@@ -60,7 +60,7 @@ public class AuthToutiaoRequest extends AuthDefaultRequest {
.remark(user.getString("description"))
.gender(AuthUserGender.getRealGender(user.getString("gender")))
.token(authToken)
.source(AuthSource.TOUTIAO)
.source(source)
.build();
}
......
......@@ -56,7 +56,7 @@ public class AuthWeChatRequest extends AuthDefaultRequest {
.uuid(openId)
.gender(AuthUserGender.getRealGender(object.getString("sex")))
.token(authToken)
.source(AuthSource.WECHAT)
.source(source)
.build();
}
......
......@@ -69,7 +69,7 @@ public class AuthWeiboRequest extends AuthDefaultRequest {
.remark(object.getString("description"))
.gender(AuthUserGender.getRealGender(object.getString("gender")))
.token(authToken)
.source(AuthSource.WEIBO)
.source(source)
.build();
}
......
......@@ -7,7 +7,8 @@
5. 优化`AuthConfig``AuthResponse`类,去掉不必要的lombonk注解,减少编译后的代码量
6. 使用lombok注解优化枚举类
7. `AuthQqRequest`增加refresh方法
8. 优化代码
8. 修复google登录无法获取用户信息的问题
9. 优化代码
### 2019/07/18
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册