美文网首页编什么程
小程序(公众号)授权给第三方平台流程梳理和实现

小程序(公众号)授权给第三方平台流程梳理和实现

作者: 沙蒿同学 | 来源:发表于2019-11-06 11:27 被阅读0次
    image.png

    整体流程

    • 在第三方平台应用上点击授权


      image.png
    • 进入授权页面


      image.png
    • 弹出微信授权页面,下方会显示第三方应用的基本信息


      image.png
    • 帐号管理员扫码,选择要授权的账号,进行授权(可自定义权限)
      image.png
    • 是否授权成功,回调页面显示

    技术实现

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

    接入在第三方平台应用上点击授权的时候会获取授权的预授权码(pre_auth_code),有效期为10分钟。

    • 调用接口地址

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

    在调用此接口前,需要先获取第三方平台的令牌(也叫接口调用凭证component_access_token)

    • 参数
    {
      "component_appid": "appid_value" 
    }
    

    后端返回参数,前端拼装请求微信url

    加入授权页面的时候,前端将后端返回的数据进行组织,点击组装后的url调整按钮,就可以弹出授权窗口。(微信做了限制,只能在第三方平台在设置的回调url地址才可以访问,其他本地地址无效

    image.png
    • 后端返回的参数
      'component_appid' => 'XXX',  //第三方平台app_id
      'pre_auth_code'   => 'pre_auth_code'  //  预授权码
      'redirect_uri'    => 'https://mp.weixin.qq.com/cgi-bin/componentloginpage',  //拼装的URL地址
      'auth_type'       =>  1,  //1 2 3  要授权的帐号类型
      'biz_appid'       =>  'xxx'  //指定授权唯一的小程序或公众号
    
    • 拼装示例

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

    用户授权,同意授权

    用户进入第三方平台授权页后,需要确认并同意将自己的公众号或小程序授权给第三方平台方,完成授权流程。此时在微信上,公众号已经授权给第三方平台了,在公众号平台上可以看到授权平台。然后第三方平台需要拿到公众号的基本信息、授权信息和执行权限,需要回调地址进行处理、保存授权信息(access_token和refresh_token)。

    回调地址处理授权信息

    这个回调地址是在第三方平台上设置的,拿到授权码(auth_code)后,使用授权码换取公众号或小程序的接口调用凭据和授权信息。

    • 调用接口为:

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

    • 参数
    {
      "component_appid":"appid_value" ,  //第三方平台 appid
      "authorization_code": "auth_code_value"  //授权码
    }
    

    返回是仅仅是授权信息(authorization_info)。authorizer_appid,authorizer_access_token,expires_in,authorizer_refresh_token以及权限id集这些数据。尚未获得公众号一些基本帐号信息(公众号名称、头像等等),这时候需要去获取授权方的帐号基本信息。

    • 调用接口为:

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

    • 参数
    {
      "component_appid":"appid_value" ,  //第三方平台 appid
      "authorizer_appid": "auth_code_value"  //授权方 appid
    }
    

    拿到信息后你就可以保存到数据库里了,整个微信公众号授权的流程就结束了,后续根据各自业务对授权信息和帐号信息进行其他业务处理就ok。

    代码示例

    $this->request()->getParams() 是封装好的获取参数的方法,可自行替代
    getComAccessToken() 是封装好的获取第三方接口调用凭证的方法,可自行替代
    httpsCurl() 是封装好的请求微信的方法,可自行替代
    WX_APP_ID 是全局参数第三方平台的app_id

    PS:这里的代码仅仅只是把整个业务流程写在一起,方便阅读,实际场景中代码当然不会这样子写

    • 获取预授权码
    /**
         * 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;
        }
    
    • 回调地址处理授权信息
    /**
         * 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);
        }
    

    相关文章

      网友评论

        本文标题:小程序(公众号)授权给第三方平台流程梳理和实现

        本文链接:https://www.haomeiwen.com/subject/xifybctx.html