欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信accessToken获取

程序员文章站 2022-03-26 10:58:52
...

获取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;
  }
}

获取是有白名单限制的,需要在公众平台的基本配置中添加

微信accessToken获取

微信accessToken获取