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

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

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

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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章