微信小程序第三方开发平台注册、授权、上传、审核、发布流程(2)- 授权

  • Post author:
  • Post category:小程序




授权(2)


https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Authorization_Process_Technical_Description.html


在这里插入图片描述



开发步骤:



一、第三方平台方获取预授权码(pre_auth_code)


① 获取component_access_token:


请求地址:

POST https://api.weixin.qq.com/cgi-bin/component/api_component_token

参数 类型 说明
component_appid string 第三方平台 appid
component_appsecret string 第三方平台 appsecret
component_verify_ticket string 微信后台推送的 ticket


注:微信端会定时推送消息到配置好的授权事件接收URL(创建三方平台时填写的,可在该三方详情中查看) 上,我们需要保存这个component_verify_ticket和 不断更新,component_verify_ticket必须保持是微信端推送的最新一个



返回结果示例:
{
  "component_access_token": "61W3mEpU66027wgNZ_MhGHNQDHnFATkDa9-2llqrMBjUwxRSNPbVsMmyD-yq8wZETSoE5NQgecigDrSHkPtIYA",
  "expires_in": 7200
}



② 获取pre_auth_code:


请求地址:

POST https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=COMPONENT_ACCESS_TOKEN

参数 类型 说明
component_access_token string 第三方平台component_access_token,不是authorizer_access_token
component_appid string 第三方平台 appid


返回结果示例:
{
  "pre_auth_code": "Cx_Dk6qiBE0Dmx4EmlT3oRfArPvwSQ-oa3NL_fwHM7VI08r52wazoZX2Rhpz1dEw",
  "expires_in": 600
}



二、引入用户进入授权页

在这里插入图片描述

第三方平台型服务商可以在自己的网站中放置“微信公众号授权”或者“小程序授权”的入口,或生成授权链接放置在移动网页中,引导公众号和小程序管理员进入授权页。

两种方式



方式一:授权注册页面扫码授权(二维码)


授权页网址为:

https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx&auth_type=xxx。



方式二:点击移动端链接快速授权(链接地址)

第三方平台型服务商可以生成授权链接,将链接通过移动端直接发给授权管理员,管理员确认后即授权



授权页网址为:

https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&no_scan=1&component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx&auth_type=xxx&biz_appid=xxxx#wechat_redirect

参数 必填 参数说明
component_appid 第三方平台方 appid
pre_auth_code 预授权码
redirect_uri 回调 URI
auth_type 要授权的帐号类型, 1 则商户扫码后,手机端仅展示公众号、2 表示仅展示小程序,3 表示公众号和小程序都展示。如果为未指定,则默认小程序和公众号都展示。第三方平台开发者可以使用本字段来控制授权的帐号类型。
biz_appid 指定授权唯一的小程序或公众号


注:auth_type、biz_appid 两个字段互斥。



三、用户确认并同意登录授权给第三方平台方

用户进入第三方平台授权页后,需要确认并同意将自己的公众号或小程序授权给第三方平台方,完成授权流程。



四、授权后回调 URI,得到授权码(authorization_code)和过期时间

授权流程完成后,授权页会自动跳转进入回调 URI,并在 URL 参数中返回授权码和过期时间(redirect_url?auth_code=xxx&expires_in=600)



五、利用授权码调用公众号或小程序的相关 API

在得到授权码后,第三方平台方可以使用授权码换取授权公众号或小程序的接口调用凭据(authorizer_access_token,也简称为令牌),再通过该接口调用凭据,按照公众号开发者文档或小程序开发文档的说明,去调用公众号或小程序相关 API。 (能调用哪些 API,取决于用户将哪些权限集授权给了第三方平台型服务商,也取决于公众号或小程序自身拥有哪些接口权限),使用 JS SDK 等能力。



① 使用授权码换取公众号或小程序的接口调用凭据和授权信息


请求地址:

POST https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=COMPONENT_ACCESS_TOKEN



请求参数说明:
参数 类型 必填 说明
component_access_token string 第三方平台component_access_token,不是authorizer_access_token
component_appid string 第三方平台 appid
authorization_code string 授权码, 会在授权成功时返回给第三方平台


POST 数据示例:
{
	"component_appid":"appid_value" ,
	"authorization_code": "auth_code_value"
}


请求成功后拿到
  • authorizer_access_token:授权方接口调用凭据(在授权的公众号或小程序具备API权限时,才有此返回值),也简称为令牌,后面调用小程序待开发的api中使用,
  • authorizer_refresh_token:接口调用凭据刷新令牌(在授权的公众号具备API权限时,才有此返回值),刷新令牌主要用于第三方平台获取和刷新已授权用户的access_token,只会在授权时刻提供,请妥善保存。 一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌
  • authorizer_appid 授权方 appid,用于获取授权方账号基本信息


② 获取授权方的帐号基本信息


请求地址:

POST https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=COMPONENT_ACCESS_TOKEN



请求参数说明:
参数 类型 必填 说明
component_access_token string 第三方平台component_access_token,不是authorizer_access_token
component_appid string 第三方平台 appid
authorizer_appid string 授权方 appid


POST 数据示例:
{
  "component_appid": "appid_value" ,
  "authorizer_appid": "auth_appid_value"
}


返回的小程序信息存数据库:

在这里插入图片描述



版权声明:本文为sunzhibin1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。