This article introduces the concept and method of obtaining Access Token under the WeChat public platform.
1. Access Token
access_token is the globally unique ticket of the public account. The public account needs to use access_token when calling each interface. Under normal circumstances, access_token is valid for 7200 seconds. Repeated acquisition will cause the last access_token to become invalid.
The public account can use AppID and AppSecret to call this interface to obtain access_token. AppID and AppSecret can be obtained in development mode (you need to be a developer and your account has no abnormal status). Note that the https protocol must be used when calling all WeChat interfaces.
Interface call request description
http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
Parameter description
Parameter |
Is it necessary |
Explanation |
grant_type |
Yes |
Get access_token and fill in client_credential |
appid |
is |
The only credential for third-party users |
secret |
is |
The unique credential key for third-party users, both appsecret |
Return instructions
Under normal circumstances, WeChat will return the following JSON data packet to the public account:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
Parameters |
Description |
##access_token | Obtained voucher |
expires_in | Voucher validity time, unit: seconds |
WeChat will return in case of error Error code and other information, the JSON data packet example is as follows (this example is an invalid AppID error):
{"errcode":40013,"errmsg":"invalid appid"}
2. AppId and AppSecret
Use the WeChat background to find advanced functions - Development mode
After becoming a developer, you can see the appid and appsecert
If there is no url and Token, you can first use the following test from Fangbei Studio to pass
URL: http://discuz.comli.com/test.php
Token: weixin
3. Obtain Access Token
The program is implemented as follows
$appid = "";
$appsecret = "";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
You can also splice the address directly in the browser address bar, and after execution, obtain The following data
{"access_token":"N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxOsVE08RfeD9lvK9lMguNG9kpIkKGZEjIf8Jv2m9fFhf8bnNa-yQH3g","expires_in":7200}
The parameter description is as follows
##Parameter
|
Description
|
##access_token
Obtained voucher |
| ##expires_in
The validity time of the voucher, unit: seconds |
Or use the official interface debugging tool, the address is:
Use the web debugging tool to debug the custom menu interface
Click to check the problem and get
In this way, you also get the access token
Attachment: Global return code description
Every time a public account calls an interface, it may get a correct or incorrect return code. Developers can debug the interface based on the return code information and troubleshoot errors.
The global return code description is as follows:
Return code |
Description |
-1 |
System busy |
0 |
Request successful |
40001 |
AppSecret error when obtaining access_token, or access_token is invalid |
40002 |
Illegal credential type |
40003 |
Illegal OpenID |
40004 |
Illegal media file type |
40005 |
Illegal file type |
40006 |
Illegal file size |
40007 |
Illegal media file id |
##40008 | Illegal message type |
40009 | Illegal image file size |
##40010
Illegal voice file size |
|
40011
Illegal video file size |
| ##40012
Illegal thumbnail file size |
| 40013
Illegal APPID |
| 40014
Illegal access_token |
##40015 |
Illegal menu type
|
40016 |
Number of illegal buttons
|
40017 |
Number of illegal buttons
|
40018 |
Illegal button name length
|
40019 |
Illegal button KEY length
|
40020 |
Illegal button URL length
|
40021 |
Illegal menu version number
|
40022 |
Number of illegal submenu levels
|
40023 |
Number of illegal submenu buttons
| ##40024 | Illegal submenu button type
| 40025 | Illegal submenu button name length
| 40026 | Illegal submenu button KEY length
| 40027 | Illegal submenu button URL length
| 40028 | Illegal custom menu user
| 40029 | Illegal oauth_code
| 40030 | Illegal refresh_token
| 40031 | Illegal openid list
| 40032 | Illegal openid list length
| 40033 | Illegal request characters, cannot contain \uxxxx format characters
| 40035 | Illegal parameters
| 40038 | Illegal request format
| 40039 | Illegal URL length
| 40050 | Illegal group id
| 40051 | The group name is illegal
| 41001 | The access_token parameter is missing
##41002 |
Missing appid parameter |
41003 |
Missing refresh_token parameter |
41004 |
Missing secret parameter |
41005 |
Missing multimedia file data |
41006 |
Missing media_id parameter |
41007 |
Missing submenu data |
41008 |
Missing oauth code |
41009 |
Missing openid |
42001 |
access_token timeout |
##42002 | refresh_token timeout |
##42003
oauth_code timeout |
|
43001
Requires GET request |
|
43002
Requires POST request |
|
43003
Requires HTTPS request |
|
43004
Requires receiver attention |
|
43005
Requires friend relationship |
|
44001
The multimedia file is empty |
| ##44002
The POST packet is empty |
| 44003
The graphic message content is empty |
| 44004
The text message content is empty |
| 45001 Multimedia file size exceeds limit |
| 45002
Message content exceeds limit |
| 45003
Title field exceeds limit |
| 45004
Description field exceeds limit |
| 45005
Link field exceeds limit |
| 45006
The picture link field exceeds the limit |
| 45007
The voice playback time exceeds the limit |
| 45008
The graphic message exceeds the limit |
| 45009
The interface call exceeds the limit |
| 45010
The number of created menus exceeds the limit |
| 45015
The reply time exceeds the limit |
| 45016
System grouping, modification is not allowed |
| 45017
The group name is too long |
| 45018
The number of groups exceeds the upper limit |
| 46001
No media data exists |
| 46002
Menu version that does not exist |
| 46003
Menu data that does not exist |
##46004 |
Non-existent user
|
47001 |
Parsing JSON/XML content error
|
48001 |
api function not authorized
|
50001 |
The user has not authorized the api
|
附:接口频率限制说明
公众号调用接口并不是无限制的。为了防止公众号的程序错误而引发微信服务器负载异常,默认情况下,每个公众号调用接口都不能超过一定限制,当超过一定限制时,调用对应接口会收到如下错误返回码:
{"errcode":45009,"errmsg":"api freq out of limit"}
各接口调用频率限制如下:
接口 |
每日限额 |
获取access_token |
2000 |
自定义菜单创建 |
1000 |
自定义菜单查询 |
10000 |
自定义菜单删除 |
1000 |
创建分组 |
1000 |
获取分组 |
1000 |
修改分组名 |
1000 |
移动用户分组 |
100000 |
上传多媒体文件 |
5000 |
下载多媒体文件 |
10000 |
发送客服消息 |
500000 |
获取带参数的二维码 |
10000 |
获取关注者列表 |
500 |
获取用户基本信息 |
5000000 |
获取网页授权access_token |
2000000 |
刷新网页授权access_token |
2000000 |
网页授权获取用户信息 |
2000000 |
更多微信公众平台开发-ACCESS TOKEN 相关文章请关注PHP中文网!
|
|