Home  >  Article  >  WeChat Applet  >  WeChat public platform development-ACCESS TOKEN

WeChat public platform development-ACCESS TOKEN

高洛峰
高洛峰Original
2017-02-28 10:14:582181browse

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}
##access_token Obtained voucherexpires_inVoucher validity time, unit: seconds
Parameters Description
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

微信公众平台开发-ACCESS TOKEN

After becoming a developer, you can see the appid and appsecert

微信公众平台开发-ACCESS TOKEN

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



##access_token##expires_in
##Parameter

Description

Obtained voucher

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

微信公众平台开发-ACCESS TOKEN

Click to check the problem and get

微信公众平台开发-ACCESS TOKEN

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:

##40008 Illegal message type40009Illegal image file size##4001040011##40012Illegal thumbnail file size40013Illegal APPID40014Illegal access_tokenIllegal menu typeNumber of illegal buttonsNumber of illegal buttonsIllegal button name lengthIllegal button KEY lengthIllegal button URL lengthIllegal menu version numberNumber of illegal submenu levelsNumber of illegal submenu buttons##40024Illegal submenu button type40025Illegal submenu button name length 40026Illegal submenu button KEY length40027Illegal submenu button URL length40028Illegal custom menu user40029Illegal oauth_code40030Illegal refresh_token40031Illegal openid list40032Illegal openid list length40033Illegal request characters, cannot contain \uxxxx format characters40035Illegal parameters40038Illegal request format40039Illegal URL length40050Illegal group id40051The group name is illegal41001The access_token parameter is missing##42002refresh_token timeout##42003430014300243003430044300544001 ##44002The POST packet is empty44003The graphic message content is empty44004The text message content is empty45001Multimedia file size exceeds limit45002Message content exceeds limit45003 Title field exceeds limit45004Description field exceeds limit45005Link field exceeds limit 45006The picture link field exceeds the limit45007The voice playback time exceeds the limit45008The graphic message exceeds the limit45009The interface call exceeds the limit45010The number of created menus exceeds the limit45015The reply time exceeds the limit45016System grouping, modification is not allowed45017The group name is too long45018The number of groups exceeds the upper limit46001No media data exists46002 Menu version that does not exist46003Menu data that does not existNon-existent userParsing JSON/XML content errorapi function not authorizedThe user has not authorized the api
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
Illegal voice file size
Illegal video file size
##40015
40016
40017
40018
40019
40020
40021
40022
40023
##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
oauth_code timeout
Requires GET request
Requires POST request
Requires HTTPS request
Requires receiver attention
Requires friend relationship
The multimedia file is empty
##46004
47001
48001
50001

 


附:接口频率限制说明

公众号调用接口并不是无限制的。为了防止公众号的程序错误而引发微信服务器负载异常,默认情况下,每个公众号调用接口都不能超过一定限制,当超过一定限制时,调用对应接口会收到如下错误返回码:

{"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中文网!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn