小程序(公众号)授权给第三方平台流程梳理和实现 |
您所在的位置:网站首页 › 微信取消小程序授权管理 › 小程序(公众号)授权给第三方平台流程梳理和实现 |
![]() 在第三方平台应用上点击授权 ![]() 进入授权页面 ![]() 弹出微信授权页面,下方会显示第三方应用的基本信息 ![]() 帐号管理员扫码,选择要授权的账号,进行授权(可自定义权限) ![]() 是否授权成功,回调页面显示 技术实现第三方平台方获取预授权码(preauthcode)在第三方平台应用上点击授权的时候会获取授权的预授权码(preauthcode),有效期为10分钟。 调用接口地址POST https://api.weixin.qq.com/cgi-bin/component/apicreatepreauthcode?componentaccesstoken=COMPONENTACCESSTOKEN 在调用此接口前,需要先获取第三方平台的令牌(也叫接口调用凭证componentaccesstoken) 参数代码语言:javascript复制{ "component_appid": "appid_value" }后端返回参数,前端拼装请求微信url加入授权页面的时候,前端将后端返回的数据进行组织,点击组装后的url调整按钮,就可以弹出授权窗口。(微信做了限制,只能在第三方平台在设置的回调url地址才可以访问,其他本地地址无效) ![]() https://mp.weixin.qq.com/cgi-bin/componentloginpage?componentappid=xxxx&preauthcode=xxxxx&redirecturi=xxxx&auth_type=xxx 用户授权,同意授权用户进入第三方平台授权页后,需要确认并同意将自己的公众号或小程序授权给第三方平台方,完成授权流程。此时在微信上,公众号已经授权给第三方平台了,在公众号平台上可以看到授权平台。然后第三方平台需要拿到公众号的基本信息、授权信息和执行权限,需要回调地址进行处理、保存授权信息(accesstoken和refreshtoken)。 回调地址处理授权信息这个回调地址是在第三方平台上设置的,拿到授权码(auth_code)后,使用授权码换取公众号或小程序的接口调用凭据和授权信息。 调用接口为:POST https://api.weixin.qq.com/cgi-bin/component/apiqueryauth?componentaccesstoken=COMPONENTACCESSTOKEN 参数代码语言:javascript复制{ "component_appid":"appid_value" , //第三方平台 appid "authorization_code": "auth_code_value" //授权码 }返回是仅仅是授权信息(authorizationinfo)。authorizerappid,authorizeraccesstoken,expiresin,authorizerrefresh_token以及权限id集这些数据。尚未获得公众号一些基本帐号信息(公众号名称、头像等等),这时候需要去获取授权方的帐号基本信息。 调用接口为:POST https://api.weixin.qq.com/cgi-bin/component/apigetauthorizerinfo?componentaccesstoken==COMPONENTACCESS_TOKEN 参数代码语言:javascript复制{ "component_appid":"appid_value" , //第三方平台 appid "authorizer_appid": "auth_code_value" //授权方 appid }拿到信息后你就可以保存到数据库里了,整个微信公众号授权的流程就结束了,后续根据各自业务对授权信息和帐号信息进行其他业务处理就ok。 代码示例$this->request()->getParams() 是封装好的获取参数的方法,可自行替代 getComAccessToken() 是封装好的获取第三方接口调用凭证的方法,可自行替代 httpsCurl() 是封装好的请求微信的方法,可自行替代 WXAPPID 是全局参数第三方平台的app_id PS:这里的代码仅仅只是把整个业务流程写在一起,方便阅读,实际场景中代码当然不会这样子写 获取预授权码代码语言:javascript复制/** * Created by 沙蒿. * @desc 获取预授权码pre_auth_code,有效期10分钟 */ public function getWxPreAuthCode() { $authType = $this->request()->getParams('auth_type') ?? 1; //1公众号授权,2小程序授权 if (empty($authType) || !in_array($authType, [1, 2])) { return $this->error(100201); } //获取第三方平台接口调用凭证 $comAccToken = $this->getComAccessToken(); //请求微信服务器获取预授权码url地址 $url = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=' . $comAccToken; // 获取授权请求二维码url地址 $reqUrl = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage'; $preAuthCode = CommonService::getInstance()->httpsCurl($url, 'post', 'json', [ 'component_appid' => WX_APP_ID ]); //组装格式,返回 $result = [ 'component_appid' => WX_APP_ID, 'pre_auth_code' => $preAuthCode['pre_auth_code'], 'redirect_uri' => $reqUrl, 'auth_type' => $authType, ]; if (!empty($bizAppId)) { $result['biz_appid'] = $bizAppId; } return $result; }回调地址处理授权信息代码语言:javascript复制/** * Created by 沙蒿. * @desc 微信授权 * 授权后回调URI,得到授权码(authorization_code)和过期时间10分钟,使用授权码换取公众号或小程序的接口调用凭据和授权信息 */ public function wxOAuth() { //接收授权码auth_code $code = $this->request()->getParams('auth_code'); //校验参数 if (empty($code)) { return $this->error(100202); } //获取第三方平台的接口调用凭证 $comAccToken = $this->getComAccessToken(); //使用授权码换取公众号或小程序的接口调用凭据和授权信息 $queryAuthUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' . $comAccToken; $authInfo = CommonService::getInstance()->httpsCurl($queryAuthUrl, 'post', 'json', [ 'component_appid' => WX_APP_ID, 'authorization_code' => $code ]); //获取授权信息 $authInfo = $authInfo['authorization_info']; $authorizerAppId = $authInfo['authorizer_appid']; //授权方appid //获取授权方的帐号基本信息 $authorizerInfoUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=' . $comAccToken; $appInfo = CommonService::getInstance()->httpsCurl($authorizerInfoUrl, 'post', 'json', [ 'component_appid' => WX_APP_ID, 'authorizer_appid' => $authorizerAppId ]); //保存授权信息和帐号信息 $this->saveWxAuth($authInfo, $appInfo); } |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |