步骤1:第三方平台方获取预授权码(pre_auth_code)
需要调用获取预授权码pre_auth_code的API
这边需要一个参数叫component_access_token,所以我们要先获取这个参数
这边又需要一个参数component_verify_ticket,所以我们还得先得到这个参数
这边需要注意一点,微信推送的信息需要解密后才能得到ComponentVerifyTicket,参考官方文档array (
'signature' => 'b77839dfcd586c38qqq10a5507c65a6d52321048',
'timestamp' => '1546963755',
'nonce' => '1363831928',
'encrypt_type' => 'aes',
'msg_signature' => '947f5cb323972abc897981a328419289a0f5af5f',
)
<xml>
<AppId><![CDATA[wxc2edd7434ad0rr34]]></AppId>
<Encrypt><![CDATA[EnD9uCCGRQ6WLQ9uY8JkgaaakTYU/+VrTuI2G1fD2yvkxyCHmo6z3IPGluqcvRMUq4ZLjKUKYVnjgF+eIm0YAUyDxe9nb6KuxRZqX5aG8Ovmg2CDJ6akbo9ht3dRFZGPgz25d7E4hljulmYGjeq510x7XJvMemDBQf06dDT+mW2abJM9Rxp28AYmBJV4uTcMakY9dcHajUBWA14gM1QMVwW+jnrbLhO6Dzlmb7GGf1+ZMy8227Lk1wsPTaGJA/6ObofBbgruG4EQjT+decle6G/mVK802nwdXFXR9DqQpEeCVjkjYCRCXrFLOOzRdGhiGsRmMLa/pPRr3SXhqN7nCFezM6BzuhlhIj7ZRPBYGwW8EK21OvBKQR03r+LWvMa1oExQq8vwky5Svr/bo9PjIDji2h7w1UNxBFTfXZMOgTU3l/RtEGrHg35p5AnbxHoH1V3mn6DqiLnmCa3TgtN6Xw==]]></Encrypt>
</xml>
官方的解密代码只能解析包含ToUserName的标签,所以要先构造下
$format = "<xml><ToUserName><![CDATA[".$arr['AppId']."]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf($format, $arr['Encrypt']);
然后就是解密了
import("WXBizMsgCrypt.wxBizMsgCrypt",EXTEND_PATH);
$pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
$msg = '';
$errCode = $pc->decryptMsg($post['msg_signature'], $post['timestamp'], $post['nonce'], $from_xml, $msg);
$errCode==0表示解密成功,得到需要的ComponentVerifyTicket
<xml>
<AppId><![CDATA[wxc2edd7123ad0ff99]]></AppId>
<CreateTime>1547035906</CreateTime>
<InfoType><![CDATA[component_verify_ticket]]></InfoType>
<ComponentVerifyTicket><![CDATA[ticket@@@ev_NEj21WV_eSdhIalw4Q_y0LO5qcfMBkMmBO432DjPly6e1n4ZjYhdwsafcIKCCLemOfcguCY6HLcFnYgCsA]]></ComponentVerifyTicket>
</xml>
未完待续。。。。
-------------------------分割线---------------------------
20190403接上
获取到component_verify_ticket后就可以调用获取第三方平台component_access_token的API得到component_access_token
//获取第三方平台component_access_token
public function token()
{
if(cache('api_component_token'))
{
$component_access_token = cache('api_component_token');
}
else
{
$url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
$post = [
"component_appid" => $this->app_id,//第三方平台appid
"component_appsecret" => $this->app_secret,//第三方平台appsecret
"component_verify_ticket" => $this->verify_ticket,//微信后台推送的ticket,此ticket会定时推送
];
$json = $this->postCurl($post,$url);
$result = json_decode($json,true);
if(!empty($result['component_access_token']))
{
$component_access_token = $result['component_access_token'];
cache('api_component_token',$component_access_token,7000);
}
else
throw new \Exception($json);
}
return $component_access_token;
}
然后调用获取预授权码pre_auth_code的API得到pre_auth_code
//获取预授权码pre_auth_code
public function create_pre_auth_code()
{
$component_access_token = $this->token();
$url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={$component_access_token}";
$post = [
"component_appid" => $this->app_id,//第三方平台方appid
];
$json = $this->postCurl($post,$url);
$result = json_decode($json,true);
if(!empty($result['pre_auth_code']))
{
$pre_auth_code = $result['pre_auth_code'];
}
else
throw new \Exception($json);
return $pre_auth_code;
}
至此,步骤一完成。
步骤2:引入用户进入授权页
第三方平台方可以在自己的网站中放置“微信公众号授权”或者“小程序授权”的入口,或生成授权链接放置在移动网页中,引导公众号和小程序管理员进入授权页。
步骤3:用户确认并同意登录授权给第三方平台方
用户进入第三方平台授权页后,需要确认并同意将自己的公众号或小程序授权给第三方平台方,完成授权流程。
步骤4:授权后回调URI,得到授权码(authorization_code)和过期时间
授权流程完成后,授权页会自动跳转进入回调URI,并在URL参数中返回授权码和过期时间(redirect_url?auth_code=xxx&expires_in=600)
步骤5:利用授权码调用公众号或小程序的相关API
在得到授权码后,第三方平台方可以使用授权码换取授权公众号或小程序的接口调用凭据(authorizer_access_token,也简称为令牌),再通过该接口调用凭据,按照公众号开发者文档或小程序开发文档的说明,去调用公众号或小程序相关API。
(能调用哪些API,取决于用户将哪些权限集授权给了第三方平台方,也取决于公众号或小程序自身拥有哪些接口权限),使用JS SDK等能力。具体请见
//使用授权码换取公众号或小程序的接口调用凭据和授权信息
public function query_auth($auth_code)
{
$component_access_token = $this->token();
$url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={$component_access_token}";
$post = [
"component_appid" => $this->app_id,//第三方平台appid
"authorization_code" => $auth_code,//授权code,会在授权成功时返回给第三方平台,详见第三方平台授权流程说明
];
$json = $this->postCurl($post,$url);
$result = json_decode($json,true);
if(!empty($result['authorization_info']))
{
$authorization_info = $result['authorization_info'];
}
else
throw new \Exception($json);
return $authorization_info;
}
接下来就可以用authorizer_access_token去进行公众号或小程序的相关操作了。