From 72bb1d826fc53879a5a870fbb30b3b826b5f0915 Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Sat, 4 Jul 2020 10:31:53 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=AE=8C=E6=88=90=20qq=20=E7=9A=84?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=20scope?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhyd/oauth/enums/scope/AuthQqScope.java | 54 +++++++++++++++++++ .../me/zhyd/oauth/request/AuthQqRequest.java | 12 ++++- 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/main/java/me/zhyd/oauth/enums/scope/AuthQqScope.java diff --git a/src/main/java/me/zhyd/oauth/enums/scope/AuthQqScope.java b/src/main/java/me/zhyd/oauth/enums/scope/AuthQqScope.java new file mode 100644 index 0000000..a317a3b --- /dev/null +++ b/src/main/java/me/zhyd/oauth/enums/scope/AuthQqScope.java @@ -0,0 +1,54 @@ +package me.zhyd.oauth.enums.scope; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * QQ 平台 OAuth 授权范围 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0.0 + * @since 1.0.0 + */ +@Getter +@AllArgsConstructor +public enum AuthQqScope implements AuthScope { + + /** + * {@code scope} 含义,以{@code description} 为准 + */ + GET_USER_INFO("get_user_info", "获取登录用户的昵称、头像、性别", true), + /** + * 以下 scope 需要申请:http://wiki.connect.qq.com/openapi%e6%9d%83%e9%99%90%e7%94%b3%e8%af%b7 + */ + GET_VIP_INFO("get_vip_info", "获取QQ会员的基本信息", false), + GET_VIP_RICH_INFO("get_vip_rich_info", "获取QQ会员的高级信息", false), + LIST_ALBUM("list_album", "获取用户QQ空间相册列表", false), + UPLOAD_PIC("upload_pic", "上传一张照片到QQ空间相册", false), + ADD_ALBUM("add_album", "在用户的空间相册里,创建一个新的个人相册", false), + LIST_PHOTO("list_photo", "获取用户QQ空间相册中的照片列表", false); + + private String scope; + private String description; + private boolean isDefault; + + public static List getDefaultScopes() { + AuthQqScope[] scopes = AuthQqScope.values(); + List defaultScopes = new ArrayList<>(); + for (AuthQqScope scope : scopes) { + if (scope.isDefault()) { + defaultScopes.add(scope); + } + } + return defaultScopes; + } + + public static List listScope() { + return Arrays.stream(AuthQqScope.values()).map(AuthQqScope::getScope).collect(Collectors.toList()); + } +} diff --git a/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java b/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java index 59cc275..a835f76 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java @@ -1,18 +1,19 @@ package me.zhyd.oauth.request; import com.alibaba.fastjson.JSONObject; -import me.zhyd.oauth.utils.HttpUtils; import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.enums.AuthResponseStatus; import me.zhyd.oauth.enums.AuthUserGender; +import me.zhyd.oauth.enums.scope.AuthQqScope; import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthResponse; import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.utils.GlobalAuthUtils; +import me.zhyd.oauth.utils.HttpUtils; import me.zhyd.oauth.utils.StringUtils; import me.zhyd.oauth.utils.UrlBuilder; @@ -121,8 +122,15 @@ public class AuthQqRequest extends AuthDefaultRequest { } return AuthToken.builder() .accessToken(accessTokenObject.get("access_token")) - .expireIn(Integer.valueOf(accessTokenObject.get("expires_in"))) + .expireIn(Integer.parseInt(accessTokenObject.getOrDefault("expires_in", "0"))) .refreshToken(accessTokenObject.get("refresh_token")) .build(); } + + @Override + public String authorize(String state) { + return UrlBuilder.fromBaseUrl(super.authorize(state)) + .queryParam("scope", this.getScopes(",", false, AuthQqScope.getDefaultScopes())) + .build(); + } } -- GitLab