前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信公众号三方平台开发【获取授权方的授权信息以及基本信息】

微信公众号三方平台开发【获取授权方的授权信息以及基本信息】

作者头像
用户2619822
发布2022-06-10 16:42:46
1.6K0
发布2022-06-10 16:42:46
举报
文章被收录于专栏:袁威

今天开始之前,先对上期的内容做一个小的补充,第三方平台在开发(即待全网发布)模式下,用来测试的微信公众号必须为第二期里说到的“授权测试公众号列表”(即测试白名单)里填写的微信公众号,否则会出现授权失败提示。

好了,继续今天的内容,授权成功后,微信上会提示授权成功:

然后,微信服务器回调上一期讲到的“回调URL”,并会在该URL参数中返回授权码(即authorization_code)和过期时间,接下来,就是利用返回的“授权码”和之前component_access_token篇讲到的“component_access_token”来换取微信公众号的接口调用凭据(authorizer_access_token和用于“authorizer_access_token”快过期时用来刷新它的authorizer_refresh_token)以及授权信息。

首先,利用“授权码”和“component_access_token”来获取接口调用凭据和授权信息: $component_access_token = $this -> get_component_access_token();

$url ="https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=".$component_access_token;

$param['component_appid'] = '第三方平台appid ';

$param['authorization_code'] = $auth_code;

$info= post_data ( $url, $param );

然后,利用上一步获取到的“授权方appid”和“component_access_token”来获取授权方的基本信息(包含账号名和账号类型):

$component_access_token = $this->get_component_access_token ();

    $url ='https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$component_access_token;

    $param ['component_appid'] = '第三方平台appid ';

    $param ['authorizer_appid'] =授权方appid;

    $data= post_data ( $url, $param );

注:上述两步中的返回结果示例和结果参数说明可前往微信开放平台查看【资源中心】中【第三方平台】下【授权流程技术说明】里的说明。

对于获取到的信息,第三方平台根据实际情况进行存储,我这里均采用的是写数据库方式存储。

完整代码

1)获取微信公众号接口调用凭据和授权信息

public  function  getAuthInfo($auth_code) {

         $component_access_token = $this ->get_component_access_token();

         $url ="https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=".$component_access_token;

         $param['component_appid'] = '第三方平台appid ';

         $param['authorization_code'] = $auth_code;

         $info = post_data ( $url, $param );

         return  $info;

      }

2)获取授权方的基本信息

public  function  getPublicInfo($authorizer_appid) {

         $component_access_token =$this->get_component_access_token ();

         $url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$component_access_token;

         $param ['component_appid'] = '第三方平台appid ';

         $param ['authorizer_appid'] =$authorizer_appid;

         $data = post_data ( $url, $param );

         return  $data;

      }

3)回调URL进行数据处理

public  function  after_auth() {

         $auth_code= I ( 'auth_code' );//获取authorization_code

         $auth_info= $this->getAuthInfo ( $auth_code );//获取微信公众号接口调用凭据和授权信息

         $public_info= $this->getPublicInfo ( $auth_info ['authorization_info']['authorizer_appid'] );//获取授权方的基本信息

         $map['public_name'] = $data ['public_name'] = $public_info ['authorizer_info'] ['user_name'];

         $data['wename'] = $public_info ['authorizer_info'] ['nick_name'];

         $data['wechat'] = $public_info ['authorizer_info'] ['alias'];

         //转换帐号类型

         if($public_info ['authorizer_info'] ['service_type_info'] ['id'] == 2) { // 服务号

                   $data['type'] = 2;

         }else { // 订阅号

                   $data['type'] = 0;

         }

         if($public_info ['authorizer_info'] ['verify_type_info'] ['id'] != - 1) { // 已认证

                   $data['type'] += 1;

         }

         $data['appid'] = $public_info ['authorization_info'] ['authorizer_appid'];

         $data['acc_time'] = date("Y-m-d H:i:s");

         $data['authorizer_refresh_token'] = $auth_info ['authorization_info']['authorizer_refresh_token'];

         $data['access_token'] = $auth_info ['authorization_info']['authorizer_access_token'];

         $data['head_img'] = $public_info ['authorizer_info'] ['head_img'];

         $data['principal_name']=$public_info['authorizer_info']['principal_name'];

         $data['qrcode_url'] = $public_info ['authorizer_info'] ['qrcode_url'];

         $data['uid']= session('user.id');//当前登录平台帐号id

         //查找当前授权的微信公众号是否已经授权过

         $info= M ( 'WechatPublic' )->where ( $map)->find ();

         //存在,则更新相关信息,反之,则新增

         if($info) {

                   M( 'WechatPublic' )->where ( $map )->save ( $data );

                   $url= U ( 'Wechat/Index/index' );

         }else{

                    M ( 'WechatPublic' )->add ( $data );

                   $url= U ( 'Wechat/Index/index' );

         }

         //授权完成,进入平台

         redirect( $url );

    }

至此,微信公众号对第三方平台的授权流程已经完成,下期开始,就是代微信公众号实现业务以及开发完成后进行全网发布部分咯,敬请期待!!!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-06-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 袁威 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档