1
0
mirror of synced 2025-12-22 18:08:12 +08:00

🐛 #2128 【开放平台】 修复公众号在由第三方平台管理时OAuth2Service授权相关报错问题

This commit is contained in:
hywr
2021-05-24 11:34:13 +08:00
committed by GitHub
parent 01ab45bc0c
commit a2e82448b8
11 changed files with 205 additions and 6 deletions

View File

@@ -0,0 +1,29 @@
package me.chanjar.weixin.open.api.impl;
import com.google.inject.Inject;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.open.api.WxOpenMpService;
import me.chanjar.weixin.open.test.ApiTestModule;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
@Guice(modules = ApiTestModule.class)
public class WxOpenMpOAuth2ServiceImplTest {
@Inject
protected WxOpenMpService wxOpenMpService;
@Test
public void buildAuthorizationUrl() {
String url = wxOpenMpService.getOAuth2Service().buildAuthorizationUrl("https://t.aaxp.cn/api/base/mp/showCode", "snsapi_userinfo", "");
System.out.println(url);
}
@Test
public void getAccessToken() throws WxErrorException {
WxOAuth2AccessToken result = wxOpenMpService.getOAuth2Service().getAccessToken("041crm0005iFJL1b2l400I0s0k4crm0z");
System.out.println(result);
}
}

View File

@@ -0,0 +1,64 @@
package me.chanjar.weixin.open.test;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.thoughtworks.xstream.XStream;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.open.api.WxOpenMaService;
import me.chanjar.weixin.open.api.WxOpenMpService;
import me.chanjar.weixin.open.api.WxOpenService;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
public class ApiTestModule implements Module {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private static final String TEST_CONFIG_XML = "test-config.xml";
@Override
public void configure(Binder binder) {
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {
if (inputStream == null) {
throw new WxRuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到请参照test-config-sample.xml文件生成");
}
TestConfigStorage config = this.fromXml(TestConfigStorage.class, inputStream);
WxOpenService service = new WxOpenServiceImpl();
service.setWxOpenConfigStorage(config);
binder.bind(TestConfigStorage.class).toInstance(config);
binder.bind(WxOpenService.class).toInstance(service);
if (config.getTestMpAppId() != null && !config.getTestMpAppId().isEmpty()) {
//如果配置了测试公众号,则构建公众号服务依赖
binder.bind(WxOpenMpService.class).toInstance(service.getWxOpenComponentService().getWxMpServiceByAppid(config.getTestMpAppId()));
} else {
log.warn("建议参照参照 test-config-sample.xml 配置测试公众号");
}
if (config.getTestMaAppId() != null && !config.getTestMaAppId().isEmpty()) {
//如果配置了测试小程序,则构建小程序服务依赖
binder.bind(WxOpenMaService.class).toInstance(service.getWxOpenComponentService().getWxMaServiceByAppid(config.getTestMaAppId()));
} else {
log.warn("建议参照参照 test-config-sample.xml 配置测试小程序");
}
} catch (IOException e) {
this.log.error(e.getMessage(), e);
}
}
@SuppressWarnings("unchecked")
private <T> T fromXml(Class<T> clazz, InputStream is) {
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", clazz);
xstream.processAnnotations(clazz);
return (T) xstream.fromXML(is);
}
}

View File

@@ -0,0 +1,16 @@
package me.chanjar.weixin.open.test;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Getter;
import lombok.Setter;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
@Getter
@Setter
@XStreamAlias("xml")
public class TestConfigStorage extends WxOpenInMemoryConfigStorage {
private String testMpAppId;
private String testMaAppId;
}

View File

@@ -1,7 +1,10 @@
<xml>
<componentAppId>第三方平台appID</componentAppId>
<componentSecret>第三方平台appsecret</componentSecret>
<componentAppSecret>第三方平台appsecret</componentAppSecret>
<componentVerifyTicket>微信服务器推送过来的ticket</componentVerifyTicket>
<componentToken>第三方平台Token</componentToken>
<componentAesKey>第三方平台EncodingAESKey</componentAesKey>
<appId>测试APPID</appId>
<testMpAppId>测试公众号</testMpAppId>
<testMaAppId>测试小程序</testMaAppId>
</xml>