WeChat Official Account Three-Party Platform Development [component_access_token]

Before we start today, let's add something I forgot to mention earlier - the third-party platform authorization process.

After obtaining component_verify_ticket (how to obtain it in the previous issue) and component_access_token (how to obtain it will be described later in this issue), we will begin to enter the formal authorization process. The specific process is roughly as follows:

  1. Obtaining the pre-authorization code (pre_auth_code): The pre-authorization code is the necessary information for the third-party platform to realize the authorization and hosting of the official account;
  2. Entering the authorization page: We can set the "WeChat Official Account Authorization" entry on our website to guide the WeChat official account administrator to enter the authorization page (the address of the authorization page includes parameters such as the appid, pre-authorization code, and callback URL of the third-party platform, such as : https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx );
  3. The WeChat official account administrator confirms and agrees to the login authorization: After the WeChat official account administrator enters the authorization page of the third-party platform, he needs to confirm and agree to authorize his WeChat official account to the third-party platform to complete the authorization process (after the authorization is completed, the authorization The page will automatically jump to the callback URL, and add the authorization code and expiration time to the parameters of the callback URL, such as: redirect_url?auth_code=xxx&expires_in=600);
  4. Use the authorization code to call the relevant API of the WeChat official account: After obtaining the authorization code, we can use the authorization code to obtain the interface call credentials (authorizer_access_token, token for short) to authorize the WeChat official account, and then we can call the credentials through this interface to call Wechat official account related APIs, so as to realize its business on behalf of the WeChat official account (which APIs we can call depends on which permission sets the WeChat official account administrator has authorized us, and of course, which permissions the WeChat official account itself has) ).

Ok! Let's enter today's topic, how to obtain the third-party platform component_access_token (the third-party platform's component_access_token is the calling credentials of the interface in the third-party platform authorization process, referred to as token. Each token has a validity period (2 hours), and it is limited , so here we need good token management, and when the token is about to expire (such as 1 hour and 55 minutes), make a refresh request again to obtain a new token. ).

After we get the component_verify_ticket pushed by the WeChat server, we need to use it as a parameter to send a request to the WeChat server to get the component_access_token.

Interface call request description

http request method: POST (please use https protocol)
https://api.weixin.qq.com/cgi-bin/component/api_component_token

Example of POST data:

{
"component_appid":"appid_value" ,
"component_appsecret": "appsecret_value",
"component_verify_ticket": "ticket_value"
}

Parameter Description
parameter illustrate
component_appid Third-party platform appid
component_appsecret Third-party platform appsecret
component_verify_ticket The ticket pushed by WeChat in the background, this ticket will be pushed regularly

component_appid and component_appsecret can be viewed on the third-party platform details page.

Back to Instructions

Under normal circumstances, WeChat will return the following json data packets:
{
"component_access_token":"61W3mEpU66027wgNZ_MhGHNQDHnFATkDa9-2llqrMBjUwxRSNPbVsMmyD-yq8wZETSoE5NQgecigDrSHkPtIYA",
"expires_in":7200
}

Description of result parameters
parameter illustrate
component_access_token Third-party platform access_token
expires_in Validity period
Specific program implementation:

public function get_component_access_token()
$res = $this->component_detail();//获取第三方平台基础信息
$last_time = $res['token_time'];//上一次component_access_token获取时间
$component_access_token = $res['component_access_token'];//获取数据查询到的component_access_token
$difference_time = $this->validity($last_time);//上一次获取时间与当前时间的时间差
//判断component_access_token是否为空或者是否超过有效期
if(empty($component_access_token) || $difference_time>7000){
$component_access_token = $this->get_component_access_token_again();
}
return $component_access_token;
}
//获取第三方平台基础信息
public function component_detail(){
$res = M('Public')->where(array('id'=>1))->find();
return $res;
}
//重新获取component_access_token
public function get_component_access_token_again(){
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
$tok = $this->component_detail();
$param ['component_appid'] = $tok['appid'];
$param ['component_appsecret'] = $tok['appsecret'];
$param ['component_verify_ticket'] = $tok['componentverifyticket'];
$data = post_data ( $url, $param );
$token['component_access_token'] = $data ['component_access_token'];
$token['token_time'] = date("Y-m-d H:i:s");
M('Public') ->where(array('id'=>1))->setField($token);
return $data['component_access_token'];
}
//获取时间差
public function validity($time){
$current_time = time();
$difference_time = $current_time - strtotime($time);
return $difference_time;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325966394&siteId=291194637