微信accessToken獲取

獲取accessToken工具 https://mp.weixin.qq.com/debug/

java獲取代碼

package com.yzf.mall.services.support.util;

import cn.hutool.core.lang.Dict;
import com.yzf.mall.core.execption.BusinessException;
import com.yzf.mall.core.utils.JsonUtils;
import io.micronaut.core.util.StringUtils;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WxCommonUtils {

  private static CloseableHttpClient httpclient = HttpClients.createDefault();

  private static final Logger LOGGER = LoggerFactory.getLogger(WxCommonUtils.class);

  //獲取token接口(GET)
  public final static String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APP_ID&secret=APP_SECRET";

  private static String CHAR_SET = "UTF-8";

  /**
   * 獲取接口訪問憑證
   *
   * @param appId 憑證
   * @param appSecret 密鑰
   * @return token
   */
  public static Token getToken(String appId, String appSecret) throws IOException {
    Token token = null;
    String requestUrl = GET_TOKEN_URL.replace("APP_ID", appId).replace("APP_SECRET", appSecret);
    // 發起GET請求獲取憑證
    String response = HttpClient.doGetString(requestUrl);
    // 相應結果轉化爲json,爲了取值
    Dict res = JsonUtils.parseBean(response, Dict.class);
    if (null != res) {
      if (StringUtils.isNotEmpty(res.getStr("errcode"))) {
        LOGGER.error("getToken failed, response = {}", response);
        throw new BusinessException("", "獲取wxToken失敗!!");
      }
      token = new Token();
      // 真正用的值是access_token,expires_in可用來設置access_token緩存時間
      token.setAccessToken(res.getStr("access_token"));
      token.setExpiresIn(res.getInt("expires_in"));
    }
    return token;
  }

  public static String doGetString(String url) throws IOException {
    HttpGet httpGet = new HttpGet(url);
    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
      HttpEntity entity = response.getEntity();
      return EntityUtils.toString(entity, CHAR_SET);
    }
  }

  public static String doPostString(String url, String body, ContentType contentType)
    throws IOException {
    HttpPost httpPost = new HttpPost(url);
    if (body != null) {
      httpPost.setEntity(new StringEntity(body, contentType));
    }
    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
      HttpEntity entity = response.getEntity();
      return EntityUtils.toString(entity, CHAR_SET);
    }
  }

}

class Token {

  private String accessToken;

  private int expiresIn;

  public String getAccessToken() {
    return accessToken;
  }

  public void setAccessToken(String accessToken) {
    this.accessToken = accessToken;
  }

  public int getExpiresIn() {
    return expiresIn;
  }

  public void setExpiresIn(int expiresIn) {
    this.expiresIn = expiresIn;
  }
}

獲取是有白名單限制的,需要在公衆平臺的基本配置中添加

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章