diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java index 2418a3148..e9376368e 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java @@ -4,6 +4,7 @@ import me.chanjar.weixin.common.api.WxImgProcService; import me.chanjar.weixin.common.api.WxOcrService; import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.bean.WxNetCheckResult; +import me.chanjar.weixin.common.enums.TicketType; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.service.WxService; import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; @@ -15,7 +16,6 @@ import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; import me.chanjar.weixin.mp.bean.result.WxMpUser; import me.chanjar.weixin.mp.config.WxMpConfigStorage; -import me.chanjar.weixin.common.enums.TicketType; import me.chanjar.weixin.mp.enums.WxMpApiUrl; import java.util.Map; @@ -32,9 +32,9 @@ public interface WxMpService extends WxService { * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN * * + * @param timestamp 时间戳 * @param nonce 随机串 * @param signature 签名 - * @param timestamp 时间戳 * @return 是否验证通过 */ boolean checkSignature(String timestamp, String nonce, String signature); @@ -82,8 +82,8 @@ public interface WxMpService extends WxService { * 获得时会检查 Token是否过期,如果过期了,那么就刷新一下,否则就什么都不干 * * - * @param forceRefresh 强制刷新 * @param type ticket类型 + * @param forceRefresh 强制刷新 * @return ticket * @throws WxErrorException . */ @@ -156,12 +156,12 @@ public interface WxMpService extends WxService { * URL格式为:https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect * * - * @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode + * @param redirectUri 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode * @param scope 应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login即可 * @param state 非必填,用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验 - * @return url + * @return url string */ - String buildQrConnectUrl(String redirectURI, String scope, String state); + String buildQrConnectUrl(String redirectUri, String scope, String state); /** *
@@ -172,8 +172,10 @@ public interface WxMpService extends WxService {
    * @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode
    * @param scope       scope
    * @param state       state
-   * @return url
+   * @return url string
+   * @deprecated use oauth2Service.buildAuthorizationUrl() instead
    */
+  @Deprecated
   String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state);
 
   /**
@@ -186,6 +188,7 @@ public interface WxMpService extends WxService {
    * @return token对象
    * @throws WxErrorException .
    */
+  @Deprecated
   WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException;
 
   /**
@@ -197,6 +200,7 @@ public interface WxMpService extends WxService {
    * @return 新的token对象
    * @throws WxErrorException .
    */
+  @Deprecated
   WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException;
 
   /**
@@ -209,6 +213,7 @@ public interface WxMpService extends WxService {
    * @return 用户对象
    * @throws WxErrorException .
    */
+  @Deprecated
   WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException;
 
   /**
@@ -219,6 +224,7 @@ public interface WxMpService extends WxService {
    * @param oAuth2AccessToken token对象
    * @return 是否有效
    */
+  @Deprecated
   boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken);
 
   /**
@@ -276,6 +282,7 @@ public interface WxMpService extends WxService {
    * 
* * @param appid 公众号的APPID + * @throws WxErrorException the wx error exception */ void clearQuota(String appid) throws WxErrorException; @@ -286,9 +293,11 @@ public interface WxMpService extends WxService { * 可以参考,{@link MediaUploadRequestExecutor}的实现方法 * * - * @param data 参数数据 + * @param the type parameter + * @param the type parameter * @param executor 执行器 * @param url 接口地址 + * @param data 参数数据 * @return 结果 * @throws WxErrorException 异常 */ @@ -297,8 +306,8 @@ public interface WxMpService extends WxService { /** * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求. * - * @param queryParam 参数 * @param url 请求接口地址 + * @param queryParam 参数 * @return 接口响应字符串 * @throws WxErrorException 异常 */ @@ -307,8 +316,8 @@ public interface WxMpService extends WxService { /** * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求. * - * @param postData 请求参数json值 * @param url 请求接口地址 + * @param postData 请求参数json值 * @return 接口响应字符串 * @throws WxErrorException 异常 */ @@ -321,9 +330,11 @@ public interface WxMpService extends WxService { * 可以参考,{@link MediaUploadRequestExecutor}的实现方法 * * - * @param data 参数数据 + * @param the type parameter + * @param the type parameter * @param executor 执行器 * @param url 接口地址 + * @param data 参数数据 * @return 结果 * @throws WxErrorException 异常 */ @@ -597,16 +608,16 @@ public interface WxMpService extends WxService { /** * . * - * @param tagService . + * @param userTagService . */ - void setTagService(WxMpUserTagService tagService); + void setUserTagService(WxMpUserTagService userTagService); /** * . * - * @param qrCodeService . + * @param qrcodeService . */ - void setQrCodeService(WxMpQrcodeService qrCodeService); + void setQrcodeService(WxMpQrcodeService qrcodeService); /** * . @@ -712,4 +723,18 @@ public interface WxMpService extends WxService { * @param commentService . */ void setCommentService(WxMpCommentService commentService); + + /** + * Gets oauth2 service. + * + * @return the oauth2 service + */ + WxOAuth2Service getOAuth2Service(); + + /** + * Sets oauth2Service. + * + * @param oAuth2Service the o auth 2 service + */ + void setOAuth2Service(WxOAuth2Service oAuth2Service); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxOAuth2Service.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxOAuth2Service.java new file mode 100644 index 000000000..1fbb1f1a9 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxOAuth2Service.java @@ -0,0 +1,72 @@ +package me.chanjar.weixin.mp.api; + +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; +import me.chanjar.weixin.mp.bean.result.WxMpUser; + +/** + * oauth2 相关接口. + * + * @author Binary Wang + * @date 2020-08-08 + */ +public interface WxOAuth2Service { + /** + *
+   * 构造oauth2授权的url连接.
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
+   * 
+ * + * @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode + * @param scope scope + * @param state state + * @return url + */ + String buildAuthorizationUrl(String redirectURI, String scope, String state); + + /** + *
+   * 用code换取oauth2的access token.
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
+   * 
+ * + * @param code code + * @return token对象 + * @throws WxErrorException . + */ + WxMpOAuth2AccessToken getAccessToken(String code) throws WxErrorException; + + /** + *
+   * 刷新oauth2的access token.
+   * 
+ * + * @param refreshToken 刷新token + * @return 新的token对象 + * @throws WxErrorException . + */ + WxMpOAuth2AccessToken refreshAccessToken(String refreshToken) throws WxErrorException; + + /** + *
+   * 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以.
+   * 
+ * + * @param oAuth2AccessToken token对象 + * @param lang zh_CN, zh_TW, en + * @return 用户对象 + * @throws WxErrorException . + */ + WxMpUser getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException; + + /** + *
+   * 验证oauth2的access token是否有效.
+   * 
+ * + * @param oAuth2AccessToken token对象 + * @return 是否有效 + */ + boolean validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken); + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java index 9aedcf0af..ee861e076 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java @@ -8,12 +8,12 @@ import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.api.WxImgProcService; -import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.api.WxOcrService; import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.bean.WxNetCheckResult; import me.chanjar.weixin.common.enums.TicketType; +import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.session.StandardSessionManager; @@ -48,36 +48,81 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*; */ @Slf4j public abstract class BaseWxMpServiceImpl implements WxMpService, RequestHttp { - - protected WxSessionManager sessionManager = new StandardSessionManager(); + @Getter + @Setter private WxMpKefuService kefuService = new WxMpKefuServiceImpl(this); + @Getter + @Setter private WxMpMaterialService materialService = new WxMpMaterialServiceImpl(this); + @Getter + @Setter private WxMpMenuService menuService = new WxMpMenuServiceImpl(this); + @Getter + @Setter private WxMpUserService userService = new WxMpUserServiceImpl(this); - private WxMpUserTagService tagService = new WxMpUserTagServiceImpl(this); - private WxMpQrcodeService qrCodeService = new WxMpQrcodeServiceImpl(this); + @Getter + @Setter + private WxMpUserTagService userTagService = new WxMpUserTagServiceImpl(this); + @Getter + @Setter + private WxMpQrcodeService qrcodeService = new WxMpQrcodeServiceImpl(this); + @Getter + @Setter private WxMpCardService cardService = new WxMpCardServiceImpl(this); + @Getter + @Setter private WxMpStoreService storeService = new WxMpStoreServiceImpl(this); + @Getter + @Setter private WxMpDataCubeService dataCubeService = new WxMpDataCubeServiceImpl(this); + @Getter + @Setter private WxMpUserBlacklistService blackListService = new WxMpUserBlacklistServiceImpl(this); + @Getter + @Setter private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this); + @Getter + @Setter private final WxMpSubscribeMsgService subscribeMsgService = new WxMpSubscribeMsgServiceImpl(this); + @Getter + @Setter private WxMpDeviceService deviceService = new WxMpDeviceServiceImpl(this); + @Getter + @Setter private WxMpShakeService shakeService = new WxMpShakeServiceImpl(this); + @Getter + @Setter private WxMpMemberCardService memberCardService = new WxMpMemberCardServiceImpl(this); + @Getter + @Setter private WxMpMassMessageService massMessageService = new WxMpMassMessageServiceImpl(this); + @Getter + @Setter private WxMpAiOpenService aiOpenService = new WxMpAiOpenServiceImpl(this); + @Getter + @Setter private final WxMpWifiService wifiService = new WxMpWifiServiceImpl(this); + @Getter + @Setter private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this); + @Getter + @Setter private WxMpCommentService commentService = new WxMpCommentServiceImpl(this); + @Getter + @Setter private WxOcrService ocrService = new WxMpOcrServiceImpl(this); + @Getter + @Setter private WxImgProcService imgProcService = new WxMpImgProcServiceImpl(this); - @Getter @Setter private WxMpMerchantInvoiceService merchantInvoiceService = new WxMpMerchantInvoiceServiceImpl(this, this.cardService); + @Getter + @Setter + private WxOAuth2Service oAuth2Service = new WxOAuth2ServiceImpl(this); + private Map configStorageMap; private int retrySleepMillis = 1000; @@ -182,9 +227,9 @@ public abstract class BaseWxMpServiceImpl implements WxMpService, RequestH } @Override - public String buildQrConnectUrl(String redirectURI, String scope, String state) { + public String buildQrConnectUrl(String redirectUri, String scope, String state) { return String.format(QRCONNECT_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(), - URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state)); + URIUtil.encodeURIComponent(redirectUri), scope, StringUtils.trimToEmpty(state)); } private WxMpOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException { @@ -199,46 +244,22 @@ public abstract class BaseWxMpServiceImpl implements WxMpService, RequestH @Override public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { - String url = String.format(OAUTH2_ACCESS_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(), - this.getWxMpConfigStorage().getSecret(), code); - return this.getOAuth2AccessToken(url); + return this.oAuth2Service.getAccessToken(code); } @Override public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { - String url = String.format(OAUTH2_REFRESH_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(), refreshToken); - return this.getOAuth2AccessToken(url); + return this.oAuth2Service.refreshAccessToken(refreshToken); } @Override public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken token, String lang) throws WxErrorException { - if (lang == null) { - lang = "zh_CN"; - } - - String url = String.format(OAUTH2_USERINFO_URL.getUrl(this.getWxMpConfigStorage()), token.getAccessToken(), token.getOpenId(), lang); - - try { - RequestExecutor executor = SimpleGetRequestExecutor.create(this); - String responseText = executor.execute(url, null, WxType.MP); - return WxMpUser.fromJson(responseText); - } catch (IOException e) { - throw new RuntimeException(e); - } + return this.oAuth2Service.getUserInfo(token,lang); } @Override public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken token) { - String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), token.getAccessToken(), token.getOpenId()); - - try { - SimpleGetRequestExecutor.create(this).execute(url, null, WxType.MP); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (WxErrorException e) { - return false; - } - return true; + return this.oAuth2Service.validateAccessToken(token); } @Override @@ -491,218 +512,9 @@ public abstract class BaseWxMpServiceImpl implements WxMpService, RequestH this.maxRetryTimes = maxRetryTimes; } - @Override - public WxMpKefuService getKefuService() { - return this.kefuService; - } - - @Override - public WxMpMaterialService getMaterialService() { - return this.materialService; - } - - @Override - public WxMpMenuService getMenuService() { - return this.menuService; - } - - @Override - public WxMpUserService getUserService() { - return this.userService; - } - - @Override - public WxMpUserTagService getUserTagService() { - return this.tagService; - } - - @Override - public WxMpQrcodeService getQrcodeService() { - return this.qrCodeService; - } - - @Override - public WxMpCardService getCardService() { - return this.cardService; - } - - @Override - public WxMpDataCubeService getDataCubeService() { - return this.dataCubeService; - } - - @Override - public WxMpUserBlacklistService getBlackListService() { - return this.blackListService; - } - - @Override - public WxMpStoreService getStoreService() { - return this.storeService; - } - - @Override - public WxMpTemplateMsgService getTemplateMsgService() { - return this.templateMsgService; - } - - @Override - public WxMpSubscribeMsgService getSubscribeMsgService() { - return this.subscribeMsgService; - } - - @Override - public WxMpDeviceService getDeviceService() { - return this.deviceService; - } - - @Override - public WxMpShakeService getShakeService() { - return this.shakeService; - } - - @Override - public WxMpMemberCardService getMemberCardService() { - return this.memberCardService; - } - @Override public RequestHttp getRequestHttp() { return this; } - @Override - public WxMpMassMessageService getMassMessageService() { - return this.massMessageService; - } - - @Override - public void setKefuService(WxMpKefuService kefuService) { - this.kefuService = kefuService; - } - - @Override - public void setMaterialService(WxMpMaterialService materialService) { - this.materialService = materialService; - } - - @Override - public void setMenuService(WxMpMenuService menuService) { - this.menuService = menuService; - } - - @Override - public void setUserService(WxMpUserService userService) { - this.userService = userService; - } - - @Override - public void setTagService(WxMpUserTagService tagService) { - this.tagService = tagService; - } - - @Override - public void setQrCodeService(WxMpQrcodeService qrCodeService) { - this.qrCodeService = qrCodeService; - } - - @Override - public void setCardService(WxMpCardService cardService) { - this.cardService = cardService; - } - - @Override - public void setStoreService(WxMpStoreService storeService) { - this.storeService = storeService; - } - - @Override - public void setDataCubeService(WxMpDataCubeService dataCubeService) { - this.dataCubeService = dataCubeService; - } - - @Override - public void setBlackListService(WxMpUserBlacklistService blackListService) { - this.blackListService = blackListService; - } - - @Override - public void setTemplateMsgService(WxMpTemplateMsgService templateMsgService) { - this.templateMsgService = templateMsgService; - } - - @Override - public void setDeviceService(WxMpDeviceService deviceService) { - this.deviceService = deviceService; - } - - @Override - public void setShakeService(WxMpShakeService shakeService) { - this.shakeService = shakeService; - } - - @Override - public void setMemberCardService(WxMpMemberCardService memberCardService) { - this.memberCardService = memberCardService; - } - - @Override - public void setMassMessageService(WxMpMassMessageService massMessageService) { - this.massMessageService = massMessageService; - } - - @Override - public WxMpAiOpenService getAiOpenService() { - return this.aiOpenService; - } - - @Override - public void setAiOpenService(WxMpAiOpenService aiOpenService) { - this.aiOpenService = aiOpenService; - } - - @Override - public WxMpWifiService getWifiService() { - return this.wifiService; - } - - @Override - public WxOcrService getOcrService() { - return this.ocrService; - } - - @Override - public WxMpMarketingService getMarketingService() { - return this.marketingService; - } - - @Override - public void setMarketingService(WxMpMarketingService marketingService) { - this.marketingService = marketingService; - } - - @Override - public void setOcrService(WxOcrService ocrService) { - this.ocrService = ocrService; - } - - @Override - public WxMpCommentService getCommentService() { - return this.commentService; - } - - @Override - public void setCommentService(WxMpCommentService commentService) { - this.commentService = commentService; - } - - @Override - public WxImgProcService getImgProcService() { - return this.imgProcService; - } - - @Override - public void setImgProcService(WxImgProcService imgProcService) { - this.imgProcService = imgProcService; - } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxOAuth2ServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxOAuth2ServiceImpl.java new file mode 100644 index 000000000..3c6287b7d --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxOAuth2ServiceImpl.java @@ -0,0 +1,95 @@ +package me.chanjar.weixin.mp.api.impl; + +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; +import me.chanjar.weixin.common.enums.WxType; +import me.chanjar.weixin.common.error.WxError; +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.common.util.http.RequestExecutor; +import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; +import me.chanjar.weixin.common.util.http.URIUtil; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.api.WxOAuth2Service; +import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; +import me.chanjar.weixin.mp.bean.result.WxMpUser; +import me.chanjar.weixin.mp.config.WxMpConfigStorage; +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; + +import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*; + +/** + * oauth2 相关接口实现类. + * + * @author Binary Wang + * @date 2020-08-08 + */ +@RequiredArgsConstructor +public class WxOAuth2ServiceImpl implements WxOAuth2Service { + private final WxMpService wxMpService; + + @Override + public String buildAuthorizationUrl(String redirectURI, String scope, String state) { + return String.format(CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(getMpConfigStorage()), + getMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state)); + } + + private WxMpOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException { + try { + RequestExecutor executor = SimpleGetRequestExecutor.create(this.wxMpService.getRequestHttp()); + String responseText = executor.execute(url, null, WxType.MP); + return WxMpOAuth2AccessToken.fromJson(responseText); + } catch (IOException e) { + throw new WxErrorException(WxError.builder().errorCode(99999).errorMsg(e.getMessage()).build(), e); + } + } + + @Override + public WxMpOAuth2AccessToken getAccessToken(String code) throws WxErrorException { + String url = String.format(OAUTH2_ACCESS_TOKEN_URL.getUrl(getMpConfigStorage()), getMpConfigStorage().getAppId(), + getMpConfigStorage().getSecret(), code); + return this.getOAuth2AccessToken(url); + } + + @Override + public WxMpOAuth2AccessToken refreshAccessToken(String refreshToken) throws WxErrorException { + String url = String.format(OAUTH2_REFRESH_TOKEN_URL.getUrl(getMpConfigStorage()), getMpConfigStorage().getAppId(), refreshToken); + return this.getOAuth2AccessToken(url); + } + + protected WxMpConfigStorage getMpConfigStorage() { + return this.wxMpService.getWxMpConfigStorage(); + } + + @Override + public WxMpUser getUserInfo(WxMpOAuth2AccessToken token, String lang) throws WxErrorException { + if (lang == null) { + lang = "zh_CN"; + } + + String url = String.format(OAUTH2_USERINFO_URL.getUrl(getMpConfigStorage()), token.getAccessToken(), token.getOpenId(), lang); + + try { + RequestExecutor executor = SimpleGetRequestExecutor.create(this.wxMpService.getRequestHttp()); + String responseText = executor.execute(url, null, WxType.MP); + return WxMpUser.fromJson(responseText); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean validateAccessToken(WxMpOAuth2AccessToken token) { + String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(getMpConfigStorage()), token.getAccessToken(), token.getOpenId()); + + try { + SimpleGetRequestExecutor.create(this.wxMpService.getRequestHttp()).execute(url, null, WxType.MP); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (WxErrorException e) { + return false; + } + return true; + } +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxOAuth2ServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxOAuth2ServiceImplTest.java new file mode 100644 index 000000000..8729f99d2 --- /dev/null +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxOAuth2ServiceImplTest.java @@ -0,0 +1,47 @@ +package me.chanjar.weixin.mp.api.impl; + +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.api.test.ApiTestModule; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import javax.inject.Inject; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * 测试类. + * + * @author Binary Wang + * @date 2020-08-09 + */ +@Test +@Guice(modules = ApiTestModule.class) +public class WxOAuth2ServiceImplTest { + @Inject + private WxMpService mpService; + + @Test + public void testBuildAuthorizationUrl() { + final String url = this.mpService.getOAuth2Service().buildAuthorizationUrl("http://www.baidu.com", "test", "GOD"); + assertThat(url).isEqualTo("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + + this.mpService.getWxMpConfigStorage().getAppId() + + "&redirect_uri=http%3A%2F%2Fwww.baidu.com&response_type=code&scope=test&state=GOD&connect_redirect=1#wechat_redirect"); + } + + @Test + public void testGetAccessToken() { + } + + @Test + public void testRefreshAccessToken() { + } + + @Test + public void testGetUserInfo() { + } + + @Test + public void testValidateAccessToken() { + } +}