第三方平台api地址
https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/预授权码获取之后调用接口获取授权方信息
https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/authorization_info.html#%E6%8E%88%E6%9D%83%E4%BF%A1%E6%81%AF%E8%AF%B4%E6%98%8E
1.微信开放平台编辑开发配置
2.授权事件接受配置>获取令牌>获取预授权码>查询预授权码
<!-- 网络请求--><dependency><groupId>com.github.lianjiatech</groupId><artifactId>retrofit-spring-boot-starter</artifactId><version>2.3.5</version></dependency><dependency><groupId>com.squareup.retrofit2</groupId><artifactId>converter-simplexml</artifactId><version>2.9.0</version></dependency>
@RetrofitClient(baseUrl = "https://api.weixin.qq.com/")
public interface WeChatApi {/*** 令牌* 文档地址 https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/component_access_token.html* 请求地址 https://api.weixin.qq.com/cgi-bin/component/api_component_token*/@POST("cgi-bin/component/api_component_token")JSONObject getComponentAccessToken(@Body GetComponentAccessTokenParam param);/*** 预授权码* <p>* 文档地址 https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/pre_auth_code.html* 请求地址 https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=COMPONENT_ACCESS_TOKEN*/@POST("/cgi-bin/component/api_create_preauthcode")JSONObject getPreAuthCode(@Query("component_access_token") String token, @Body GetPreAuthCodeParam param);/*** 查询授权方接口* https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=COMPONENT_ACCESS_TOKEN*/@POST("/cgi-bin/component/api_query_auth")JSONObject getApiQueryAuth(@Query("component_access_token") String accessToken,@Body GetApiQueryAuthParam param);
}
3.授权事件接收配置
@ApiOperation("授权事件接收配置")@RequestMapping("/component_verify_ticket")@ResponseBodypublic String componentVerifyTicket(@RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("msg_signature") String msgSignature,@RequestBody String postData) throws DocumentException, AesException {//https://sunuping.com/vt/api/wx/open/component_verify_ticketreturn this.memberWxService.componentVerifyTicket(postData, msgSignature, timestamp, nonce);}@Overridepublic String componentVerifyTicket(String data, String msgSignature, String timestamp, String nonce) throws DocumentException, AesException {if (log.isDebugEnabled()) {log.debug(data);}//这个类是微信官网提供的解密类,需要用到消息校验Token 消息加密Key和服务平台appidWXBizMsgCrypt pc = new WXBizMsgCrypt(WxConfigConstant.TOKEN, WxConfigConstant.KEY, WxConfigConstant.APPID);String xml = pc.decryptMsg(msgSignature, timestamp, nonce, data);Map<String, String> map = XmlTools.getMap(xml);final String d = map.get("ComponentVerifyTicket");if (log.isDebugEnabled()) {log.debug("获取微信验证票据:{}", d);}if (StringUtils.isNotBlank(d)) {//缓存11小时redisService.set(WxRedisKeyConstant.COMPONENT_VERIFY_TICKET, d, 39600);}return "success";}
4.获取第三方api授权token
@Overridepublic String getComponentAccessToken() {String componentAccessToken = (String) redisService.get(WxRedisKeyConstant.COMPONENT_ACCESS_TOKEN);if (StringUtils.isBlank(componentAccessToken)) {final String ticket = Optional.ofNullable((String) redisService.get(WxRedisKeyConstant.COMPONENT_VERIFY_TICKET)).orElseThrow(() -> new ErrorException("验证凭据获取失败"));JSONObject tokenJson = Optional.ofNullable(weChatApi.getComponentAccessToken(new GetComponentAccessTokenParam(WxConfigConstant.APPID, WxConfigConstant.APP_SECRET, ticket)))//.orElseThrow(() -> new ErrorException("令牌获取失败"));if (log.isDebugEnabled()) {log.debug(tokenJson.toJSONString());}componentAccessToken = Optional.ofNullable(tokenJson.getString("component_access_token")).orElseThrow(() -> new ErrorException("令牌数据为空"));//缓存1小时50分钟redisService.set(WxRedisKeyConstant.COMPONENT_ACCESS_TOKEN, componentAccessToken, tokenJson.getIntValue("expires_in"));}return componentAccessToken;}
5.获取预授权码
@Overridepublic String getPreAuthCode() {final String componentAccessToken = this.getComponentAccessToken();String code = (String) this.redisService.get(WxRedisKeyConstant.PRE_AUTH_CODE);if (StringUtils.isBlank(code)) {JSONObject preAuthCodeJson = Optional.ofNullable(weChatApi.getPreAuthCode(componentAccessToken, new GetPreAuthCodeParam(WxConfigConstant.APPID)))//.orElseThrow(() -> new ErrorException("获取预授权码失败"));if (log.isDebugEnabled()) {log.debug(preAuthCodeJson.toJSONString());}code = Optional.ofNullable(preAuthCodeJson.getString("pre_auth_code")).orElseThrow(() -> new ErrorException("获取预授权码失败"));this.redisService.set(WxRedisKeyConstant.PRE_AUTH_CODE, code, preAuthCodeJson.getIntValue("expires_in"));}return this.generatePreAuthCodeUrl(code);}private String generatePreAuthCodeUrl(String code) {long mid = StpUtil.getLoginIdAsLong();//https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx&auth_type=xxxreturn "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=" + WxConfigConstant.APPID + "&pre_auth_code=" + code//授权完回去的页面 授权回去的域名要去配置的域名一样+ "&redirect_uri=https://xxxx/x/bind_wx_public?mid=" + mid + "&auth_type=1";}
6.拉取授权方信息
@GetMapping("/bind_wx_public")@ApiOperation("绑定微信公众号")public void bindWxPublic(@RequestParam("auth_code") String authCode, @RequestParam("expires_in") Integer expiresIn, @RequestParam("mid") Long mid) throws IOException {this.memberWxService.bindWxPublic(authCode, expiresIn, mid);}@Overridepublic void bindWxPublic(String authCode, Integer expiresIn, Long mid) throws IOException {log.debug("授权码:{},失效时间/秒:{}", authCode, expiresIn);final String componentAccessToken = this.getComponentAccessToken();JSONObject apiQueryAuthJson = this.weChatApi.getApiQueryAuth(componentAccessToken, new GetApiQueryAuthParam(WxConfigConstant.APPID, authCode));log.debug("授权方信息:{}", apiQueryAuthJson.toJSONString());JSONObject authorizationInfo = apiQueryAuthJson.getJSONObject("authorization_info");//授权方 appidString authorizerAppid = authorizationInfo.getString("authorizer_appid");//接口调用令牌String authorizerAccessToken = authorizationInfo.getString("authorizer_access_token");//authorizer_access_token 的有效期int authorizerAccessTokenExpiresIn = authorizationInfo.getIntValue("expires_in");//刷新令牌String authorizerRefreshToken = authorizationInfo.getString("authorizer_refresh_token");//授权给开发者的权限集列表JSONArray funcInfoJsonArr = authorizationInfo.getJSONArray("func_info");//业务处理完后 重定向到某个页面response.sendRedirect("https://xxxxx");}