WeChat development - get basic interface access_token

Access Token

 

The access_token is the globally unique ticket of the official account, and the access_token is required when the official account calls each interface. Developers need to keep it properly.

 

The storage of the access_token must retain at least 512 characters of space, and the validity period is currently 7200 seconds. It needs to be refreshed regularly. Repeated acquisition will invalidate the access_token obtained last time.

 

The official account can use the AppID and AppSecret to call the WeChat interface to obtain the access_token. AppID and AppSecret can be obtained from the official website of WeChat public platform - development - basic configuration (you need to have become a developer and the account has no abnormal status). The https protocol is required when calling all WeChat APIs.

 

Special Note

This article obtains the Access Token required to call the basic interface, not the Access Token used for web page authorization (implemented through the OAuth2.0 mechanism). Please note the difference.

 



Get Access Token

 

method one:

 

def getAccessToken()
{
    def APPID = "wxe49d******43c1cd"
    def SECRET = "217c05ff85************db8f4c9371"
    def requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET"
    
    URL url = new URL(requestUrl)
    def params = "APPID=" + URLEncoder.encode(APPID, 'UTF-8') +
                 "&SECRET=" + URLEncoder.encode(SECRET, 'UTF-8')

    HttpURLConnection connection = (HttpURLConnection) url.openConnection()
    connection.setDoOutput(true)
    connection.setRequestMethod("POST")
    connection.outputStream.withWriter { Writer writer -> writer.write params }
    def response = connection.inputStream.withReader { Reader reader -> reader.text }
    def accessToken = JSON.parse(response).getAt("access_token")
    
    return accessToken
}

 

Method Two:

 

Access Token can be obtained directly through the browser






Method three:

 

Obtain Access Token by calling the tool through the WeChat public platform interface

https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E5%9F%BA%E7%A1%80%E6%94%AF%E6%8C%81&form=%E8%8E%B7%E5%8F%96access_token%E6%8E%A5%E5%8F%A3%20/token

 


 

Interface Frequency Limit Description

 

The public account call interface is not unlimited. In order to prevent the program error of the official account from causing abnormal load on the WeChat server, by default, the call interface of each official account cannot exceed a certain limit. When the limit is exceeded, the corresponding interface will receive the following error return code:

{"errcode":45009,"errmsg":"api freq out of limit"}

各接口限制的调用次数可通过微信公众平台官网-开发-接口权限 中查询到。



 

 

Guess you like

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