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

本文介绍微信公众号第三方平台授权过程中的预授权码(pre_auth_code)。该码用于安全验证,有效期20分钟。获取预授权码需组件访问令牌(component_access_token)及组件应用ID(component_appid),并提供获取代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁威

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值