微信公眾號三方平台開發【獲取授權方的授權信息以及基本信息】


今天開始之前,先對上期的內容做一個小的補充,第三方平台在開發(即待全網發布)模式下,用來測試的微信公眾號必須為第二期里說到的“授權測試公眾號列表”(即測試白名單)里填寫的微信公眾號,否則會出現授權失敗提示。

好了,繼續今天的內容,授權成功后,微信上會提示授權成功:
授權成功提示

然后,微信服務器回調上一期講到的“回調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 );
}

至此,微信公眾號對第三方平台的授權流程已經完成,下期開始,就是代微信公眾號實現業務以及開發完成后進行全網發布部分咯,敬請期待!!!


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com