微信公众号三方平台(基于Thinkphp框架)

微信公众号三方平台开发【pre_auth_code篇】

2017-05-31  本文已影响476人  袁小威

节后第一天班,目测很多小伙伴还沉浸在假日里无法自拔(偷笑),勤劳的袁小威今天继续给大家放大招~~~

今天,给大家讲的是进入授权页需要的一个重要参数,预授权码。

预授权码(即pre_auth_code)是在微信公众号给第三方平台授权时,用来安全验证的,有效时间为20分钟(之前文档说的有效期为10分钟,现在又改成了20分钟,但是返回结果示例里貌似又是10分钟,这个大家根据实际情况随机应变即可)。这一步我们需要用到的参数是component_access_token和component_appid。

接口调用请求说明

http请求方式: POST(请使用https协议)
https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=xxx

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,对于刚接触第三方平台的开发者来说,可能又是一个坑。下期我会就这块写下我的开发步骤,当然完整的代码都是会贴出来滴,届时可供大家参考,嗯,今天就先这样咯,下期见~

上一篇下一篇

猜你喜欢

热点阅读