From 42ede32fc5d5a5e17e9517efa9fc0c798a69072d Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Tue, 18 Jun 2019 19:51:53 +0800 Subject: [PATCH] =?UTF-8?q?:beers:=20=E9=86=89=E9=85=92=E5=86=99=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/me/zhyd/oauth/model/AuthToken.java | 1 + .../java/me/zhyd/oauth/request/AuthQqRequest.java | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/zhyd/oauth/model/AuthToken.java b/src/main/java/me/zhyd/oauth/model/AuthToken.java index 96f1d2c..37dcfd4 100644 --- a/src/main/java/me/zhyd/oauth/model/AuthToken.java +++ b/src/main/java/me/zhyd/oauth/model/AuthToken.java @@ -19,6 +19,7 @@ public class AuthToken { private String uid; private String openId; private String accessCode; + private String unionId; /** * Google附带属性 diff --git a/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java b/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java index 868e56e..bdbe341 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java @@ -48,7 +48,7 @@ public class AuthQqRequest extends BaseAuthRequest { @Override protected AuthUser getUserInfo(AuthToken authToken) { String accessToken = authToken.getAccessToken(); - String openId = this.getOpenId(accessToken); + String openId = this.getOpenId(authToken); HttpResponse response = HttpRequest.get(UrlBuilder.getQqUserInfoUrl(config.getClientId(), accessToken, openId)) .execute(); JSONObject object = JSONObject.parseObject(response.body()); @@ -73,7 +73,8 @@ public class AuthQqRequest extends BaseAuthRequest { .build(); } - private String getOpenId(String accessToken) { + private String getOpenId(AuthToken authToken) { + String accessToken = authToken.getAccessToken(); HttpResponse response = HttpRequest.get(UrlBuilder.getQqOpenidUrl("https://graph.qq.com/oauth2.0/me", accessToken)) .execute(); if (response.isOk()) { @@ -82,10 +83,12 @@ public class AuthQqRequest extends BaseAuthRequest { String removeSuffix = StrUtil.replace(removePrefix, ");", ""); String openId = StrUtil.trim(removeSuffix); JSONObject object = JSONObject.parseObject(openId); - if (object.containsKey("openid")) { - return object.getString("openid"); + if (object.containsKey("error")) { + throw new AuthException(object.get("error") + ":" + object.get("error_description")); } - throw new AuthException("Invalid openId"); + authToken.setOpenId(object.getString("openid")); + authToken.setUnionId(object.getString("unionid")); + return authToken.getOpenId(); } throw new AuthException("Invalid openId"); } -- GitLab