🐛 #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

@@ -1,8 +1,9 @@
package me.chanjar.weixin.open.api;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.open.bean.WxOpenCreateResult;
import me.chanjar.weixin.open.bean.WxOpenGetResult;
import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
@@ -330,7 +331,10 @@ public interface WxOpenComponentService {
* @param code the code
* @return the wx mp o auth 2 access token
* @throws WxErrorException the wx error exception
* @see WxMpService#getOAuth2Service()
* @deprecated 2021-05-21: 已修正公众号相关接口,请使用:WxOpenCommpentService.getWxMpServiceByAppid(mpAppId).getOAuth2Service().getAccessToken(code)
*/
@Deprecated
WxOAuth2AccessToken oauth2getAccessToken(String appid, String code) throws WxErrorException;
/**
@@ -362,7 +366,10 @@ public interface WxOpenComponentService {
* @param scope the scope
* @param state the state
* @return the string
* @see WxMpService#getOAuth2Service()
* @deprecated 2021-05-21: 已修正公众号相关接口,请使用:WxOpenCommpentService.getWxMpServiceByAppid(mpAppId).getOAuth2Service().buildAuthorizationUrl(redirectUri, scope, state)
*/
@Deprecated
String oauth2buildAuthorizationUrl(String appid, String redirectUri, String scope, String state);
/**

View File

@@ -0,0 +1,61 @@
package me.chanjar.weixin.open.api.impl;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.common.service.WxOAuth2ServiceDecorator;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.open.api.WxOpenComponentService;
import org.apache.commons.lang3.StringUtils;
/**
* 微信 第三方平台对于公众号 oauth2 的实现类
*
* @author <a href="https://www.sacoc.cn">广州跨界</a>
*/
public class WxOpenMpOAuth2ServiceImpl extends WxOAuth2ServiceDecorator {
private final WxOpenComponentService wxOpenComponentService;
private final WxMpConfigStorage wxMpConfigStorage;
public WxOpenMpOAuth2ServiceImpl(WxOpenComponentService wxOpenComponentService, WxOAuth2Service wxOAuth2Service, WxMpConfigStorage wxMpConfigStorage) {
super(wxOAuth2Service);
this.wxOpenComponentService = wxOpenComponentService;
this.wxMpConfigStorage = wxMpConfigStorage;
}
/**
* 第三方平台代公众号发起网页授权
* 文档地址:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html
*
* @param code 微信授权code
* @return 微信用户信息
* @throws WxErrorException 如果微信接口调用失败将抛出此异常
*/
@Override
public WxOAuth2AccessToken getAccessToken(String code) throws WxErrorException {
String url = String.format(
WxOpenComponentService.OAUTH2_ACCESS_TOKEN_URL,
wxMpConfigStorage.getAppId(),
code,
wxOpenComponentService.getWxOpenConfigStorage().getComponentAppId()
);
String responseContent = wxOpenComponentService.get(url);
return WxOAuth2AccessToken.fromJson(responseContent);
}
@Override
public String buildAuthorizationUrl(String redirectUri, String scope, String state) {
return String.format(
WxOpenComponentService.CONNECT_OAUTH2_AUTHORIZE_URL,
wxMpConfigStorage.getAppId(),
URIUtil.encodeURIComponent(redirectUri),
scope,
StringUtils.trimToEmpty(state),
wxOpenComponentService.getWxOpenConfigStorage().getComponentAppId()
);
}
}

View File

@@ -10,7 +10,6 @@ import me.chanjar.weixin.open.api.WxOpenMpService;
import me.chanjar.weixin.open.bean.mp.FastRegisterResult;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Objects;
/**
@@ -22,9 +21,11 @@ public class WxOpenMpServiceImpl extends WxMpServiceImpl implements WxOpenMpServ
private String appId;
public WxOpenMpServiceImpl(WxOpenComponentService wxOpenComponentService, String appId, WxMpConfigStorage wxMpConfigStorage) {
// wxOpenComponentService.oauth2getAccessToken(appId)
this.wxOpenComponentService = wxOpenComponentService;
this.appId = appId;
this.wxMpConfigStorage = wxMpConfigStorage;
setOAuth2Service(new WxOpenMpOAuth2ServiceImpl(wxOpenComponentService, getOAuth2Service(), wxMpConfigStorage));
initHttp();
}