From 1c30f6ab2f1bccee93ad55868e277802e758cce1 Mon Sep 17 00:00:00 2001
From: "yadong.zhang"
Date: Tue, 25 Jun 2019 19:32:18 +0800
Subject: [PATCH] =?UTF-8?q?:art:=20=E9=80=82=E9=85=8Dqq=E6=8E=88=E6=9D=83?=
=?UTF-8?q?=E7=99=BB=E5=BD=95=E6=97=B6=E5=BC=80=E5=8F=91=E8=80=85=E8=B4=A6?=
=?UTF-8?q?=E5=8F=B7=E6=B2=A1=E6=9C=89=E7=94=B3=E8=AF=B7unionId=E6=9D=83?=
=?UTF-8?q?=E9=99=90=E8=80=8C=E5=AF=BC=E8=87=B4=E6=8A=A5=E9=94=99=E7=9A=84?=
=?UTF-8?q?=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 6 +++---
pom.xml | 2 +-
src/main/java/me/zhyd/oauth/config/AuthConfig.java | 7 +++++++
.../java/me/zhyd/oauth/request/AuthQqRequest.java | 6 ++++--
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java | 11 ++++++-----
update.md | 3 +++
6 files changed, 24 insertions(+), 11 deletions(-)
diff --git a/README.md b/README.md
index 34bcaff..af432e0 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@
-
+
@@ -65,7 +65,7 @@ JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具
me.zhyd.oauth
JustAuth
- 1.7.0
+ 1.7.1
```
- 调用api
@@ -106,7 +106,7 @@ authRequest.login("code");
| | [AuthTaobaoRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTaobaoRequest.java) | 参考文档 |
| | [AuthGoogleRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java) | 参考文档 |
| | [AuthFacebookRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java) | 参考文档 |
-| | [AuthDouyinRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java) | 参考文档 |
+| | [AuthDouyinRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java) | 参考文档 |
| | [AuthLinkedinRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java) | 参考文档 |
| | [AuthMicrosoftRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java) | 参考文档 |
| | [AuthMiRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java) | 参考文档 |
diff --git a/pom.xml b/pom.xml
index 4cacc20..826a658 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
me.zhyd.oauth
JustAuth
- 1.7.0
+ 1.7.1
JustAuth
https://gitee.com/yadong.zhang/JustAuth
diff --git a/src/main/java/me/zhyd/oauth/config/AuthConfig.java b/src/main/java/me/zhyd/oauth/config/AuthConfig.java
index 9bf98ab..3ea5554 100644
--- a/src/main/java/me/zhyd/oauth/config/AuthConfig.java
+++ b/src/main/java/me/zhyd/oauth/config/AuthConfig.java
@@ -35,4 +35,11 @@ public class AuthConfig {
* 支付宝公钥:当选择支付宝登录时,该值可用
*/
private String alipayPublicKey;
+
+ /**
+ * 是否需要申请unionid,目前只针对qq登录
+ * 注:qq授权登录时,获取unionid需要单独发送邮件申请权限。如果个人开发者账号中申请了该权限,可以将该值置为true,在获取openId时就会同步获取unionId
+ * 参考链接:http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D
+ */
+ private boolean unionId;
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java b/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java
index d9ad586..b0e2915 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java
@@ -85,7 +85,7 @@ public class AuthQqRequest extends BaseAuthRequest {
private String getOpenId(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
- HttpResponse response = HttpRequest.get(UrlBuilder.getQqOpenidUrl("https://graph.qq.com/oauth2.0/me", accessToken))
+ HttpResponse response = HttpRequest.get(UrlBuilder.getQqOpenidUrl("https://graph.qq.com/oauth2.0/me", accessToken, config.isUnionId()))
.execute();
if (response.isOk()) {
String body = response.body();
@@ -97,7 +97,9 @@ public class AuthQqRequest extends BaseAuthRequest {
throw new AuthException(object.get("error") + ":" + object.get("error_description"));
}
authToken.setOpenId(object.getString("openid"));
- authToken.setUnionId(object.getString("unionid"));
+ if (object.containsKey("unionid")) {
+ authToken.setUnionId(object.getString("unionid"));
+ }
return StringUtils.isEmpty(authToken.getUnionId()) ? authToken.getOpenId() : authToken.getUnionId();
}
diff --git a/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java b/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
index 7437cac..2fdd309 100644
--- a/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+++ b/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
@@ -58,7 +58,7 @@ public class UrlBuilder {
private static final String QQ_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}";
private static final String QQ_USER_INFO_PATTERN = "{0}?oauth_consumer_key={1}&access_token={2}&openid={3}";
private static final String QQ_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}";
- private static final String QQ_OPENID_PATTERN = "{0}?access_token={1}&unionid=1";
+ private static final String QQ_OPENID_PATTERN = "{0}?access_token={1}&unionid={2}";
private static final String WECHAT_AUTHORIZE_PATTERN = "{0}?appid={1}&redirect_uri={2}&response_type=code&scope=snsapi_login&state={3}#wechat_redirect";
private static final String WECHAT_ACCESS_TOKEN_PATTERN = "{0}?appid={1}&secret={2}&code={3}&grant_type=authorization_code";
@@ -438,12 +438,13 @@ public class UrlBuilder {
/**
* 获取qq授权地址
*
- * @param url 获取qqopenid的api接口地址
- * @param token qq 应用授权的token
+ * @param url 获取qqopenid的api接口地址
+ * @param token qq 应用授权的token
+ * @param unionid 是否需要获取unionid,默认为false。注:获取unionid需要单独发送邮件申请权限,请个人视情况而定。参考链接:http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D
* @return full url
*/
- public static String getQqOpenidUrl(String url, String token) {
- return MessageFormat.format(QQ_OPENID_PATTERN, url, token);
+ public static String getQqOpenidUrl(String url, String token, boolean unionid) {
+ return MessageFormat.format(QQ_OPENID_PATTERN, url, token, unionid ? 1 : 0);
}
/**
diff --git a/update.md b/update.md
index 7441705..3a37de9 100644
--- a/update.md
+++ b/update.md
@@ -1,3 +1,6 @@
+### 2019/06/25
+qq授权登录时,需要获取`openId`作为`uuid`,在`1.6.1-beta`和`1.7.0`版本中,引入了`unionId`这一属性。获取`unionid`需要单独向qq团队**发送邮件**申请权限,鉴于这一申请权限的步骤比较麻烦(需要填写的内容比较多),所以在`AuthConfig`中增加了一个`unionId`属性,当为**true**时才会获取unionid,当为false时只获取openId。如果你需要该功能, 则在自行申请了相关权限后,将该属性置为true即可。关于unionId的参考链接:[UnionID介绍](http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D)
+
### 2019/06/19
1. 合并[xkcoding](https://github.com/xkcoding)提交的[PR](https://github.com/zhangyd-c/JustAuth/pull/14),重构了部分代码,jar包由原来的`130+kb`优化到现在的`110+kb`
2. 合并[skqing](https://gitee.com/skqing)提交的[PR](https://gitee.com/yadong.zhang/JustAuth/pulls/3), 解决抖音登录失败问题
--
GitLab