From 07bc5a5eac07e49c70847eda029b608f31a19eb4 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Mon, 19 Oct 2020 00:18:15 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E5=BE=AE=E4=BF=A1=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E6=A8=A1=E5=9D=97=E5=A2=9E=E5=8A=A0OAuth2?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=EF=BC=88=E7=BD=91=E9=A1=B5?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E3=80=81=E7=BD=91=E9=A1=B5=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=AD=89=EF=BC=89=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/impl/WxOpenOAuth2ServiceImpl.java | 79 +++++++++++++++++++ .../api/impl/WxOpenOAuth2ServiceImplTest.java | 38 +++++++++ 2 files changed, 117 insertions(+) create mode 100644 weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImpl.java create mode 100644 weixin-java-open/src/test/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImplTest.java diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImpl.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImpl.java new file mode 100644 index 000000000..82b0f419e --- /dev/null +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImpl.java @@ -0,0 +1,79 @@ +package me.chanjar.weixin.open.api.impl; + +import lombok.AllArgsConstructor; +import me.chanjar.weixin.common.bean.WxOAuth2UserInfo; +import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken; +import me.chanjar.weixin.common.enums.WxType; +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.common.error.WxRuntimeException; +import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; +import me.chanjar.weixin.common.util.http.URIUtil; +import me.chanjar.weixin.mp.api.WxOAuth2Service; +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; + +import static me.chanjar.weixin.mp.enums.WxMpApiUrl.OAuth2.*; + +/** + * oauth2接口实现. + * + * @author Binary Wang + * @date 2020-10-19 + */ +@AllArgsConstructor +public class WxOpenOAuth2ServiceImpl extends WxOpenServiceImpl implements WxOAuth2Service { + private final String appId; + private final String appSecret; + + @Override + public String buildAuthorizationUrl(String redirectUri, String scope, String state) { + return String.format(CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(null), + this.appId, URIUtil.encodeURIComponent(redirectUri), scope, StringUtils.trimToEmpty(state)); + } + + private WxOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException { + return WxOAuth2AccessToken.fromJson(this.get(url, null)); + } + + @Override + public WxOAuth2AccessToken getAccessToken(String code) throws WxErrorException { + return this.getAccessToken(this.appId, this.appSecret, code); + } + + @Override + public WxOAuth2AccessToken getAccessToken(String appId, String appSecret, String code) throws WxErrorException { + return this.getOAuth2AccessToken(String.format(OAUTH2_ACCESS_TOKEN_URL.getUrl(null), appId, appSecret, code)); + } + + @Override + public WxOAuth2AccessToken refreshAccessToken(String refreshToken) throws WxErrorException { + String url = String.format(OAUTH2_REFRESH_TOKEN_URL.getUrl(null), this.appId, refreshToken); + return this.getOAuth2AccessToken(url); + } + + @Override + public WxOAuth2UserInfo getUserInfo(WxOAuth2AccessToken token, String lang) throws WxErrorException { + if (lang == null) { + lang = "zh_CN"; + } + + String url = String.format(OAUTH2_USERINFO_URL.getUrl(null), token.getAccessToken(), token.getOpenId(), lang); + + return WxOAuth2UserInfo.fromJson(this.get(url, null)); + } + + @Override + public boolean validateAccessToken(WxOAuth2AccessToken token) { + String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(null), token.getAccessToken(), token.getOpenId()); + + try { + SimpleGetRequestExecutor.create(this).execute(url, null, WxType.MP); + } catch (IOException e) { + throw new WxRuntimeException(e); + } catch (WxErrorException e) { + return false; + } + return true; + } +} diff --git a/weixin-java-open/src/test/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImplTest.java b/weixin-java-open/src/test/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImplTest.java new file mode 100644 index 000000000..17387fcc1 --- /dev/null +++ b/weixin-java-open/src/test/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImplTest.java @@ -0,0 +1,38 @@ +package me.chanjar.weixin.open.api.impl; + +import org.testng.annotations.Test; + +import static org.testng.Assert.*; + +/** + * 单元测试. + * + * @author Binary Wang + * @date 2020-10-19 + */ +public class WxOpenOAuth2ServiceImplTest { + + @Test + public void testBuildAuthorizationUrl() { + } + + @Test + public void testGetAccessToken() { + } + + @Test + public void testTestGetAccessToken() { + } + + @Test + public void testRefreshAccessToken() { + } + + @Test + public void testGetUserInfo() { + } + + @Test + public void testValidateAccessToken() { + } +}