小程序 - Access Token

https://mp.weixin.qq.com/wiki?action=doc&id=mp1421140183

Access Token 是一個"公众号的全局唯一接口",也就是一個公众号會有一個,

取得方法相當簡單,就是發出一個 GET 請求,並帶上 appId、appSecret,

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

根據文檔裡面的回傳值,我們可以定義 access token 如下,並加入一個 expire 函式

public class ResAccessToken {
    String access_token;
    Integer expires_in;
    Date createDate;
    Long errcode;
    String errmsg;

    public boolean isExpired() {
        final Date date = this.createDate != null ? this.createDate : new Date(0L);
        final int second = this.expires_in;
        final Date expireDate = DateUtils.addSeconds(date, second);
        final Date now = new Date();
        return now.after(expireDate);
    }
}

並且在服務器端加一個 map 當作 local cache,避免一直重複取值。

results matching ""

    No results matching ""