Skip to content

Commit

Permalink
fix(UNI-160): get access token using test mp's clientid and secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-Tian committed Aug 24, 2023
1 parent 0a50394 commit fce0fc3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services-social-weixin</artifactId>
<version>0.3.6</version>
<version>0.3.8</version>
<name>Keycloak Services Social WeiXin</name>
<description/>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public List<ProviderConfigProperty> getConfigProperties() {
.label("PC 登录 URL")
.helpText("PC 登录 URL 的登录页面,可以配置为一个自定义的前端登录页面,用来展示公众号带参二维码")
.type(ProviderConfigProperty.STRING_TYPE)
.add().build();
.add()

.property().name(WeiXinIdentityProvider.WMP_APP_ID)
.label("小程序 appId")
.helpText("小程序的 appid")
.type(ProviderConfigProperty.STRING_TYPE)
.add()
.property().name(WeiXinIdentityProvider.WMP_APP_SECRET)
.label("小程序 appSecret")
.helpText("小程序的 app secret")
.type(ProviderConfigProperty.STRING_TYPE)
.add()

.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ public WeixinIdentityCustomAuth(KeycloakSession session, OAuth2IdentityProviderC

// TODO: cache mechanism
public String getAccessToken(WechatLoginType wechatLoginType) throws IOException {
logger.info("getAccessToken with " + wechatLoginType);

var clientId = this.getConfig().getClientId();
var clientSecret = this.getConfig().getClientSecret();

try {
String ua = session.getContext().getRequestHeaders().getHeaderString("user-agent").toLowerCase();
logger.info("ua = " + ua);

if (!isWechatBrowser(ua) || WechatLoginType.FROM_PC_QR_CODE_SCANNING.equals(wechatLoginType)) {
logger.info("not wechat browser or from pc qr code scanning");

clientId = this.getConfig().getConfig().get(WECHAT_MP_APP_ID);
clientSecret = this.getConfig().getConfig().get(WECHAT_MP_APP_SECRET);
}
Expand All @@ -44,7 +49,7 @@ public String getAccessToken(WechatLoginType wechatLoginType) throws IOException
logger.info(String.format("getAccessToken by %s%n%s%n", clientId, clientSecret));
var res =
SimpleHttp.doGet(String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid=%s&secret=%s", this.getConfig().getClientId(), this.getConfig().getClientSecret()),
"&appid=%s&secret=%s", clientId, clientSecret),
this.session).asString();

logger.info(String.format("res is %s%n", res));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.keycloak.social.weixin.egress.wechat.mp;

public class AccessTokenRequestBody {
public String grant_type;
public String appid;
public String secret;

public AccessTokenRequestBody(String grant_type, String appid, String secret) {
this.grant_type = grant_type;
this.appid = appid;
this.secret = secret;
}
}

0 comments on commit fce0fc3

Please sign in to comment.