mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 11:35:12 +08:00
新增 微信第三方平台 使用授权码获取授权信息接口
This commit is contained in:
@@ -9,6 +9,7 @@ import com.jsowell.common.util.wxplatform.AesException;
|
||||
import com.jsowell.common.util.wxplatform.WXBizMsgCrypt;
|
||||
import com.jsowell.common.util.wxplatform.WXXmlToMapUtil;
|
||||
import com.jsowell.pile.dto.GetComponentTokenDTO;
|
||||
import com.jsowell.pile.vo.agentDev.AuthInfo;
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.slf4j.Logger;
|
||||
@@ -124,6 +125,7 @@ public class AgentDevService {
|
||||
|
||||
/**
|
||||
* 小程序/公众号授权事件
|
||||
*
|
||||
* @param map
|
||||
*/
|
||||
private void authorizedInform(Map<String, String> map) {
|
||||
@@ -209,4 +211,44 @@ public class AgentDevService {
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用授权码获取授权信息
|
||||
* 并将授权信息中的 接口调用令牌、刷新令牌存入缓存
|
||||
*
|
||||
* @param authorizationCode
|
||||
*/
|
||||
public AuthInfo getAuthInfoByAuthCode(String authorizationCode) {
|
||||
// 获取 component_access_token
|
||||
GetComponentTokenDTO dto = GetComponentTokenDTO.builder()
|
||||
.appId(PLATFORM_APP_ID)
|
||||
.appSecret(PLATFORM_APP_SECRET)
|
||||
.verifyTicket(null)
|
||||
.build();
|
||||
String componentToken = getComponentToken(dto);
|
||||
// 使用授权码获取授权信息 url
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentToken;
|
||||
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("component_appid", PLATFORM_APP_ID);
|
||||
jsonObject.put("authorization_code", authorizationCode);
|
||||
|
||||
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));
|
||||
logger.info("获取第三方平台 使用授权码获取授权信息 请求参数:{}, 请求结果:{}", JSONObject.toJSONString(jsonObject), result);
|
||||
AuthInfo authInfo = JSONObject.parseObject(result, AuthInfo.class);
|
||||
if (authInfo == null) {
|
||||
throw new RuntimeException("获取第三方平台 使用授权码获取授权信息 error");
|
||||
}
|
||||
String authorizerAccessToken = authInfo.getAuthorizerAccessToken(); // 接口调用令牌, 默认有效期 7200 秒
|
||||
String authorizerRefreshToken = authInfo.getAuthorizerRefreshToken(); // 刷新令牌 永久保存
|
||||
String authorizerAppid = authInfo.getAuthorizerAppid(); // 授权方 appid
|
||||
|
||||
String authAccessTokenKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + authorizerAppid;
|
||||
redisCache.setCacheObject(authAccessTokenKey, authorizerAccessToken, authInfo.getExpiredTime(), TimeUnit.SECONDS);
|
||||
|
||||
String authRefreshTokenKey = CacheConstants.AUTHORIZER_REFRESH_TOKEN + authorizerAppid;
|
||||
redisCache.setCacheObject(authRefreshTokenKey, authorizerRefreshToken);
|
||||
|
||||
return authInfo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user