1. 程式人生 > 其它 >微信公眾號開發之Access token(二)

微信公眾號開發之Access token(二)

自定義選單需要獲取Access token

公眾號使用AppID和AppSecret呼叫介面來獲取access_token
公眾號和小程式均可以使用AppID和AppSecret呼叫本介面來獲取access_token。AppID和AppSecret可在“微信公眾平臺-開發-基本配置”頁中獲得

access_token是公眾號的全域性唯一介面呼叫憑據,公眾號呼叫各介面時都需使用access_token。開發者需要進行妥善儲存。access_token的儲存至少要保留512個字元空間。access_token的有效期目前為2個小時,需定時重新整理,重複獲取將導致上次獲取的access_token失效。

access_token的有效期是7200秒(兩小時),在有效期內,可以一直使用,只有當access_token過期時,才需要再次呼叫介面 獲取access_token。在理想情況下,一個7x24小時執行的系統,每天只需要獲取12次access_token,即每2小時獲取一次。如果在 有效期內,再次獲取access_token,那麼上一次獲取的access_token將失效。
目前,獲取access_token介面的呼叫頻率限制為2000次/天,如果每次傳送客服訊息、獲取使用者資訊、群發訊息之前都要先呼叫獲取 access_token介面得到介面訪問憑證,這顯然是不合理的,一方面會更耗時(多了一次介面呼叫操作),另一方面2000次/天的呼叫限制恐怕也不 夠用。因此,在實際應用中,我們需要將獲取到的access_token儲存起來,然後定期呼叫access_token介面更新它,以保證隨時取出的 access_token都是有效的。

原文連結

介面呼叫請求說明

https請求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

引數說明

引數 說明
grant_type 獲取access_token填寫client_credential
appid 第三方使用者唯一憑證
secret 第三方使用者唯一憑證金鑰,即appsecret

返回說明

正常情況下,微信會返回下述JSON資料包給公眾號:

{"access_token":"ACCESS_TOKEN","expires_in":7200}
引數說明

引數 說明
access_token 獲取到的憑證
expires_in 憑證有效時間,單位:秒

封裝這兩個引數

package com.rzk.pojo;

import lombok.Data;

@Data
public class Token {
    private String accessToken;
    private int expiresIn;
}

HttpConstant

package com.rzk.util;
public class HttpConstant {

    //獲取Access token URI
    public static String API_URI = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
}

HttpClient 工具類

package com.rzk.util;

import com.rzk.pojo.Token;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClient {

    /**
     * GET請求
     * @param url
     * @return
     */
    public static String doGetRequest(String url) {
        String result = "";
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            HttpEntity responseEntity = response.getEntity();
            result = EntityUtils.toString(responseEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }return result;
    }
}

WxServerController

    @GetMapping(value = "accessToken")
    public String AccessToken(){
        Token token = new Token();
        //使用httpclient請求
        String result = HttpClient.doGetRequest(HttpConstant.API_URI.replace("APPID", environment.getProperty("wx.appid")).replace("APPSECRET", environment.getProperty("wx.secret")));
        //轉成json物件
        JSONObject json = JSON.parseObject(result);
        token.setAccessToken(String.valueOf(json.get("access_token")));

        return token.getAccessToken();
    }

請求accessToken

還要去公眾號基礎設定開啟白名單訪問列表,填上你的伺服器ip地址即可