1. 程式人生 > >微信公眾號三方平臺開發【pre_auth_code篇】

微信公眾號三方平臺開發【pre_auth_code篇】

今天,給大家講的是進入授權頁需要的一個重要引數,預授權碼。

預授權碼(即pre_auth_code)是在微信公眾號給第三方平臺授權時,用來安全驗證的,有效時間為20分鐘(之前文件說的有效期為10分鐘,現在又改成了20分鐘,但是返回結果示例裡貌似又是10分鐘,這個大家根據實際情況隨機應變即可)。這一步我們需要用到的引數是component_access_token和component_appid。

介面呼叫請求說明
POST資料示例

{
"component_appid":"appid_value"
}

請求引數說明
引數 說明
component_appid 第三方平臺方appid
返回結果示例

{
"pre_auth_code":"Cx_Dk6qiBE0Dmx4EmlT3oRfArPvwSQ-oa3NL_fwHM7VI08r52wazoZX2Rhpz1dEw",
"expires_in":600
}

結果引數說明
引數 說明
pre_auth_code 預授權碼
expires_in 有效期,為20分鐘
具體程式實現

Public function get_pre_auth_code(){
$res = $this->component_detail();
$last_time = $res['pre_time'];
$pre_auth_code = $res['pre_auth_code'];
$difference_time = $this->validity($last_time);
if(empty($pre_auth_code) || $difference_time>1100){
$pre_auth_code = $this->get_pre_auth_code_again();
}
return $pre_auth_code;
}
Public function get_pre_auth_code_again(){
$component_access_token = $this ->get_component_access_token();
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token='.$component_access_token;
$param ['component_appid'] = $tt['appid'];
$data = post_data ( $url, $param );
$pre['pre_auth_code']= $data ['pre_auth_code'];
$pre['pre_time'] = date("Y-m-d H:i:s");
M('Public') ->where(array('id'=>1))->setField($pre);
return $data ['pre_auth_code'];
}

在這一步獲取到pre_auth_code後,接下來需要做的就是生成授權頁,對於授權頁具體生成和呼叫這塊,微信官方是沒有給出具體的說明文件的,只在“授權流程技術說明”的最前面簡單的給了一個示例URL,對於剛接觸第三方平臺的開發者來說,可能又是一個坑。下期我會就這塊寫下我的開發步驟,當然完整的程式碼都是會貼出來滴,屆時可供大家參考。