Public platform development-access_token acquisition and application (including source code), Public Account accesstoken
Public platform development-access_token acquisition and application (including source code)
Author: Meng xianglei-public platform development example tutorial
Many systems have the access_token parameter. For the access_token parameter of the public platform, the server determines the permission of the public platform and allows or disables the public platform from performing the current operation.
I. Functions and Use Cases of access_token
(1) functions of access_token
Access_token is composed of the AppID and AppSecret of the public account, so it can identify the public account.
(2) Use Cases of access_token
It can be compared to a key. Only the public account can call the interface through access_token, such as the server IP address acquisition interface, interfaces for getting a list of users who are interested and getting basic user information.
The server also uses access_token to determine whether the public account has the permission to call this interface. For example, when you call the Server IP address to obtain an interface, you must include the access_token parameter. After receiving the request, the system first checks whether the public account of the access_token has the interface permission to obtain the Server IP address. If yes, returns the queried data. If not, the public account is returned without the permission to obtain the Server IP interface.
(3) features of access_token
The access_token storage must retain at least 512 characters. The validity period of access_token is currently 2 hours. Repeated access_token acquisition will invalidate the previous access_token.
(4) access_token storage call Policy
Access_token is a policy to be adopted in later application development. The access_token is stored on the central control server. All programs that need this parameter should access the Central Control Server to obtain the access_token, the Central Control Server checks whether the current access_token is valid and refreshes the token.
(5) obtain the access_token Value
1. Interface Description
Http Request Method: GET
Interface call address:
Https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = APPID & secret = APPSECRET
The call parameters are described in the following table:
Parameters |
Required? |
Description |
Grant_type |
Yes |
Obtain access_token and enter client_credential |
Appid |
Yes |
Unique third-party user credential |
Secret |
Yes |
The unique credential key of a third-party user, that is, appsecret |
Return description:
Normally, a JSON data packet is returned to the Public Account:
{& Quot; access_token & quot;: & quot; ACCESS_TOKEN & quot;, & quot; expires_in & quot;: 7200}
Response parameters:
Parameters |
Description |
Access_token |
Obtained credential |
Expires_in |
Valid credential time, in seconds |
When an error occurs, an error code or other information is returned. An example of a JSON data packet is as follows (this example is an invalid AppID error ):
{& Quot; errcode & quot;: 40013, & quot; errmsg & quot;: & quot; invalid appid & quot "}
2. Parameters AppId and AppSecret
Two very important parameters, AppId and AppSecret, are used to obtain access_token. You can view them in development-basic configuration.
3. Get access_token
<? Php/** get token * // 1. contains wei_function.php require ('wei _ function. php '); $ appid = "wx78478e595939c538"; $ secret = "5540e8ccab4f71dfad752f73cfb85780"; // 2. get the token address and replace $ appid and $ secret $ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ appid. "& secret = ". $ secret. ""; // 3. call the getdata () function $ output = getdata ($ url) in the wei_function.php file; // 4. json_decode decodes JSON data into an array and uses $ token = object_array (json_decode ($ output); // outputs the array // print_r ($ token); // 5. output access_token echo $ token ['Access _ token'];?>
Code Parsing
Require ('wei _ function. php'); this function file can be purchased for the public platform development instance tutorial, which is detailed on page 1 of this book.
: