在学习微信第三方平台开发之前你应该会的:
1,常用接口测试工具的使用:postman
2,学会看第三方平台文档。(这个很关键,不过很多还是喜欢百度,白嫖别人的操作文档)
3,学习排查问题,分析问题。
4,第三方平台的限制,和公众号,订阅号这些关系要了解。测试订阅号没有认证,没有认证的订阅号区别都是很大的。
5,善于使用缓存解决项目当中业务性能瓶颈。
微信开放文档:微信公众平台开发概述 | 微信开放文档
开盘:
上一个流程图:

第三方平台相当于代理我们微信公众号的配置,通过第三方平台的配置发起事件监听,包含回调信息处理,我们通过:授权事件接收配置--》开启推送Ticket--》授权后实现业务--》消息校验Token--》消息与事件接收配置--》微信公众号业务
流程就是这么简单,但是接口比较多,操作起来比较麻烦,还得管理token实效。我们最终要获取微信公众号的权限token才能实现业务扭转,要不然其他都是扯淡。
我们来看看第三方平台的页面配置和权限:

我这是自己的映射域名,正式上线后换成正式域名。
权限配置:

需要调试的接口:
接口比较多我们nacos配置:
- wechat:
- config:
- #appid
- component_appid: *************
- #aeskey
- component_encodingaeskey: ****************
- #验证token
- component_token: ****************
- #加密正式
- component_appsecret: ***********
- #授权域名
- redirect_url: http://d1989091s1.51mypc.cn/wechat/preauth/callback
- #获取component_token url地址
- api_component_url: https://api.weixin.qq.com/cgi-bin/component/api_component_token
- #创建预授权 url地址
- api_create_preauth_url: https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=
- #权限查询api 授权平台地址
- api_query_auth_url: https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=
- #验证token 刷新地址
- api_authorizer_token_url: https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=
- #媒体文件下载地址
- api_media_download_url: https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
- #客服回复用户文本信息
- api_message_custom_send_url: https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=
- #获取刷新token接口地址
- api_refresh_url: https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=
- #openid获取 unionid地址
- api_unionid_batchget_url: https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=
- #获取用户信息
- api_unionid_get_url: https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

代码:
1,获取第三方平台授权component_token
2,获取预授权api_create_preauthcode
3,微信授权平台后获取权限接口根据预授权token+扫码授权code (api_query_auth)
4,扫码成功获取queryauthcode
5,通过queryauthcode获取公众号authorizer_token
6,通过authorizer_token操作公众号业务。
请求地址:https://api.weixin.qq.com/cgi-bin/component/api_component_token
请求参数:
- {
- "component_appid":"********",
- "component_appsecret":"****",
- "component_verify_ticket":"ticket@@@lKkaZ1rc4AL43879Kq7g4vmz8DrLdjWNtpnBoUl5tEJtcLhuSGTdayh4rZHxmRv12YB7CU_s7yQ"
- }

"errmsg": "access clientip is not registered requestIP: 58.246.190.94 rid: 62b67820-316cbaee-50f9473e"
需要配置ip白名单:


请求参数:
- {
-
- "component_appid":"wx6ec5488b62c"
-
- }

- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
-
-
- </head>
- <body>
- <div class="" style="text-align:center;padding-top:10%;font-size:10em;">
- <a href=https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=wx6ec5e8cec&pre_auth_code=preauthcode@@@YWPYsD65xHdHMkKkSoXTx8W5sXhg3j0l_QfGx_r5lADHryjDH9nEn80ILQb-TBp0pq8Gn5&redirect_uri=http://d1989091s1.51mypc.cn/wechat/preauth/callback id="url">点击授权</a>
- </div>
- <script type="text/javascript" src="js/jquery.min.js"></script>
- <script type="text/javascript" >
- $(function () {
- $.ajax({
- url: "/wechat/auth/cache",
- type: "post",
- success: function (r) {
- if (r.code == 0) {
- var component_access_token = r.data.component_access_token;
- var component_appid = r.data.component_appid;
- var pre_auth_code = r.data.pre_auth_code;
- var redirect_uri = r.data.redirect_uri;
-
- var url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid='+component_appid
- +'&pre_auth_code='+pre_auth_code+'&redirect_uri='+redirect_uri;
- $('#url').attr('href',url);
- } else {
- alert(r.msg)
- }
- }
- });
- })
-
-
-
-
- </script>
-
-
- </body>
- </html>
授权的页面:这里是需要管理员,谁认证的订阅号,就谁扫码,如果你没有公众号订阅号没有绑定,那就授权不了。



这里的权限是我们第三方平台开启的权限集需要什么功能就开放什么权限功能。
<a href=https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=wx6ec5e8cec&pre_auth_code=preauthcode@@@YWPYsD65xHdHMkKkSoXTx8W5sXhg3j0l_QfGx_r5lADHryjDH9nEn80ILQb-TBp0pq8Gn5&redirect_uri=http://d1989091s1.51mypc.cn/wechat/preauth/callback id="url">点击授权</a>
## 回调地址
redirect_uri=http://d1989091s1.51mypc.cn/wechat/preauth/callback
回调地址会返回一个auth_code默认一个小时过期,为了防止重复扫码,你必须获取到刷新token,定时刷缓存,保证token不用过期。
http://d1989091s1.51mypc.cn/wechat/preauth/callback?auth_code=queryauthcode@@@*****&expires_in=3600
参数:
{
"component_appid":"********" ,
"authorization_code": "queryauthcode@@@KEm1qXN8KFuVWO05a3FhXhMtAGQp8RFA4dAEaldQ_ffdvsKIUtDWpVXtzu8dDH8D1DCMhAS61RmonKgPBjecUw"
}
响应:
- {
- "authorization_info": {
- "authorizer_appid": "*********",
- "authorizer_access_token": "58_PZeVA66EDTONWgd-8X9Miuu2dqFQ9FzFar4JTLahB5QmCIdSE1gY9BCYQVnJ8peVGm-1231L3oyIDZ92xlbpSbESN0aIlGW-6XfJXvDIWP6b_b-WW2yEM20AZkkHnECQHDMPiD0Uzqh6sCoHrVDjRPCcAFDVTG",
- "expires_in": 7200,
- "authorizer_refresh_token": "refreshtoken@@@UFuvwOxnZ0eXBGEghR9CD12DVTQnaZEwIQYBBGc6Oh89npg",
- "func_info": [
- {
- "funcscope_category": {
- "id": 1
- },
- "confirm_info": {
- "need_confirm": 1,
- "already_confirm": 0,
- "can_confirm": 1
- }
- },
- {
- "funcscope_category": {
- "id": 2
- },
- "confirm_info": {
- "need_confirm": 1,
- "already_confirm": 0,
- "can_confirm": 1
- }
- },
- {
- "funcscope_category": {
- "id": 4
- }
- },
- {
- "funcscope_category": {
- "id": 6
- }
- },
- {
- "funcscope_category": {
- "id": 7
- }
- },
- {
- "funcscope_category": {
- "id": 9
- }
- },
- {
- "funcscope_category": {
- "id": 15
- }
- }
- ]
- }
- }
这个权限token默认2个小时过期,所以这里需要定时刷新获取权限token
authorizer_refresh_token 代表我们定时刷新权限token,这个可以帮我们解决重复扫码的问题,管理员只需要扫码一次就好了。
请求地址:
请求参数:
authorizer_appid 需要授权的第三方订阅号appid
component_appid 第三方平台appid
- {
- "component_appid":"wx6ec5e8ceb4882c",
- "authorizer_appid":"wx3605b881a5f30b22"
- }

请求参数:
- {
-
- "component_appid":"*******",
-
- "authorizer_appid":"wxbe11fb37fd31d2cb",
-
- "authorizer_refresh_token":"refreshtoken@@@UFuvwOxnZ0eXBGEghR9DVTQnaZEwIQYBBGc6Oh89npg"
-
- }
-

authorizer_access_token 验证成功的token就是我们公众号需要获取的凭证。
到这里我们就可以根据公众号的业务进行接口直接的调用了。整体的流程和实现已经完成了。