微信开放平台对接详解 阳光穿透心脏的1/2处 2022-09-14 00:07 1268阅读 0赞 ### 微信开放平台对接详解 ### * 1. 开放平台对接概述 * * 1.1 开放平台概述 * 1.2 开放平台对比 * 2. 开放平台对接流程 * * 2.1 开放平台环境 * 2.2 开放平台操作 * 2.3 开放平台授权 * 3. 微信公众号对接流程 # 1. 开放平台对接概述 # ## 1.1 开放平台概述 ## 微信三方平台指南: [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/product/Third\_party\_platform\_appid.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_product_Third_party_platform_appid.html] ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16] ## 1.2 开放平台对比 ## 由于业务需要对公司不同微信公众号进行统一管理,所以要管理的是微信公众号,但是需要注意的是要对接微信开放平台,而不是对接微信公众号。 这里首先要明白对接微信公众号与对接微信开放平台的区别,微信提供了微信公众平台与微信开放平台,微信公众平台就是微信公众号,而微信开放平台就是指的代公众号,是为第三方平台服务。 如果你针对微信公众号进行过开发,那么你肯定曾在微信公众号后台配置过自己的域名服务器以及密钥等信息。基于这个配置之后针对该公众进行定制化的开发,例如自动登录、个人中心、自定义二维码等。当你需要针对第二个微信公众号进行开发时,你可能仍然需要进行重复的步骤,并且配置不同的域名以及密钥等,于此同时每一次开发你需要保存公众号的账号密码,并需要获取公众号所有者的授权,不安全且繁琐。 随着需要运维公众号的数量逐步增加,需要投入的资源以及成本同步增加(不同公众号需要不同的域名以及服务器资源)。此时你可能需要一个平台,能够同时管理这么多公众号。因为无论具体的业务是什么场景,基于微信的开发必然是基于微信底层提供的各种接口进行不同业务形态的展开。为此我们可以通过抽象出功能层以及业务层来解构每个微信公众号的开发,其中功能层即微信提供的基础功能(回复消息/微信菜单管理/分组管理等),业务层即不同的业务场景。那么功能层就变成了第三方平台,一次开发供N个公众号使用,提供标准化的接口服务来满足业务的基础需求,业务层基于第三方平台进行其他更深的业务拓展,例如搭建CMS投放系统。 所谓对接微信开放平台就是将公众号授权给第三方平台进行管理,第三方平台拥有全部或部分的该公众号的接口权限,可以协助运营人员批量管理运营公众号。 微信开放平台本质上扮演的是一个对接微信的服务开发商角色,不管是公司内部还是三方方式提供微信公众号以及小程序等微信生态产品的服务。 微信文档相对比较详细,但是对于新手流程不够清晰,在此梳理记录。 # 2. 开放平台对接流程 # ## 2.1 开放平台环境 ## 通过导读可以了解到,三方平台首先需要成为服务商,那如何成为服务商: [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/getting\_started/how\_to\_be.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_getting_started_how_to_be.html] ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16 1]流程中1-5步骤需要在微信开放平台完成,6是开发联调,7是发布测试。 只有申请全网发布的账号才能在生产环境操作微信公众号与小程序,未通过全网发布的第三方平台,仅“授权测试公众号/小程序列表”内公众号或小程序才可进行授权,以进行测试 1、[注册开放平台以及完成开发者资质认证][Link 1] 2、[创建第三方平台][Link 2] 3、[申请全网发布][Link 3] 4、[更多操作指南请查看操作指引纵览][Link 4] ## 2.2 开放平台操作 ## 微信三方平台概述: [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/product/Third\_party\_platform\_appid.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_product_Third_party_platform_appid.html] 在上述环境准备完成以后,包括开放平台注册账号,完成开发者资质认证,创建三方平台账号,申请全网发布全部完成以后,下边要做的是: 1.注册开放平台 [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/operation/open/create.html][Link 1] ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16 2] 2.绑定公众号和小程序 [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/operation/open/bind.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_operation_open_bind.html] ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16 3] ## 2.3 开放平台授权 ## 授权流程技术说明:[https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/api/Before\_Develop/Authorization\_Process\_Technical\_Description.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_Before_Develop_Authorization_Process_Technical_Description.html] 关于授权这块微信文档流程不是很清晰,画了个序列图便于理解,后续所有的业务操作都是基于此授权展开的,所以这块很重要。 ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16 4]整体步骤说明 1.验证票据 这个是在微信开放平台配置的用于接收URL,用于接收验证票据。 验证票据(component\_verify\_ticket),在第三方平台创建审核通过后,微信服务器会向其 ”授权事件接收URL” 每隔 10 分钟以 POST 的方式推送 component\_verify\_ticket。 [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/api/ThirdParty/token/component\_verify\_ticket.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_component_verify_ticket.html] 2.第三方平台接口的调用凭据 这一步是通过验证票据获取component\_access\_token过程。 令牌(component\_access\_token)是第三方平台接口的调用凭据。令牌的获取是有限制的,每个令牌的有效期为 2 小时,请自行做好令牌的管理,在令牌快过期时(比如1小时50分),重新调用接口获取。 [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/api/ThirdParty/token/component\_access\_token.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_component_access_token.html] 3.预授权码 通过component\_access\_token获取预授权码的过程。 预授权码(pre\_auth\_code)是第三方平台方实现授权托管的必备信息,每个预授权码有效期为 1800秒。需要先获取令牌才能调用。使用过程中如遇到问题,可在开放平台服务商专区发帖交流。 [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/api/ThirdParty/token/pre\_auth\_code.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_pre_auth_code.html] 4.生成二维码所在网址 这里强调一下预授权码干嘛用呢,授权,怎么授权呢,公众号管理员扫码授权,那怎么扫码呢,通过一下地址,获取到预授权码以后,可以通过让用户(微信管理员)访问 https://mp.weixin.qq.com/cgi-bin/componentloginpage?component\_appid=xxx&pre\_auth\_code=xxx&redirect\_uri=xxx 参数说明: component\_appid component\_appid pre\_auth\_code 预授权码 redirect\_uri 回调页面 5.用户扫码授权,获取授权码 用户扫码完后,点击授权之后会进入设定的授权回调页面 redirect\_url?auth\_code=xxx&expires\_in=600。url附带参数auth\_code(授权码)和expires\_in(有效期)。 [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/api/Before\_Develop/Authorization\_Process\_Technical\_Description.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_Before_Develop_Authorization_Process_Technical_Description.html] 6.使用授权码获取授权信息 通过授权码获取授权信息,包括authorizer\_access\_token与authorizer\_refresh\_token。 [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/api/ThirdParty/token/authorization\_info.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_authorization_info.html] 7.刷新授权方令牌 这一步就是刷新令牌,保持令牌有效性,比较令牌获取次数有限不能每次调用,存储缓存,定时刷新。 [https://developers.weixin.qq.com/doc/oplatform/Third-party\_Platforms/2.0/api/ThirdParty/token/api\_authorizer\_token.html][https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_api_authorizer_token.html] # 3. 微信公众号对接流程 # 微信公众号对接相对于开放平台简单一些,是针对公众号的定制化开发, 整个开发过程通过appid与secret获取access\_token,定时摔性能token,即可展开业务操作,以下连接为官方文档可以参考: 微信公众平台开发概述:[https://developers.weixin.qq.com/doc/offiaccount/Getting\_Started/Overview.html][https_developers.weixin.qq.com_doc_offiaccount_Getting_Started_Overview.html] [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_product_Third_party_platform_appid.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/product/Third_party_platform_appid.html [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16]: /images/20220828/1aa9a541fdfa4bb5a5fa5f2a9a28af4b.png [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_getting_started_how_to_be.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/getting_started/how_to_be.html [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16 1]: /images/20220828/7530a55417904c01980a53bb18f12a14.png [Link 1]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/operation/open/create.html [Link 2]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/operation/thirdparty/apply.html [Link 3]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/operation/thirdparty/publish.html [Link 4]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/operation/overview.html [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16 2]: /images/20220828/72850bf036d040098ba814fa72f6a4cd.png [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_operation_open_bind.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/operation/open/bind.html [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16 3]: /images/20220828/ceef796b69d34166be223626f0e4390a.png [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_Before_Develop_Authorization_Process_Technical_Description.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Authorization_Process_Technical_Description.html [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6Z2W6IqC5YWI55Sf_size_20_color_FFFFFF_t_70_g_se_x_16 4]: /images/20220828/6f2e7693363a4337ae3bcd862810ef4e.png [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_component_verify_ticket.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/component_verify_ticket.html [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_component_access_token.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/component_access_token.html [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_pre_auth_code.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/pre_auth_code.html [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_authorization_info.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/authorization_info.html [https_developers.weixin.qq.com_doc_oplatform_Third-party_Platforms_2.0_api_ThirdParty_token_api_authorizer_token.html]: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_authorizer_token.html [https_developers.weixin.qq.com_doc_offiaccount_Getting_Started_Overview.html]: https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
还没有评论,来说两句吧...