目录

前言

先来看看获取token的说明:

access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。

建议公众号开发者使用中控服务器统一获取和刷新Access_token,其他业务逻辑服务器所使用的access_token均来自于该中控服务器,不应该各自去刷新,否则容易造成冲突,导致access_token覆盖而影响业务。

所以获取到的token最好缓存到服务器,免得不稳定。

微信公众号开发 - 5.获取access_token

获取access会返回两个参数:access_token 获取到的凭证、expires_in 凭证有效时间,单位:秒

创建返回参数的实体类

package cn.notemi.po;

/**
 * Title:AccessToken
 * Description:获取token返回json的实体类
 *
 * @author Flicker
 * @create 2017-07-26 下午 5:40
 **/
public class AccessToken {
    private String token ;
    private int expiresIn;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int getExpiresIn() {
        return expiresIn;
    }

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

微信工具类

获取access_token是使用get提交。

package cn.notemi.util;

import cn.notemi.po.AccessToken;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * Title:Weixin工具类
 * Description:
 *
 * @author Flicker
 * @create 2017-07-26 下午 5:22
 **/
public class WeixinUtil {
    private static final String APPID = "wx77af2e536ce6f01b";
    private static final String APPSECRET = "959776297c6246ff6fd99ac06f4e4d89";

    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    /**
     * GET请求
     * @param url
     * @return
     */
    public static JSONObject doGetStr(String url)throws Exception{
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        JSONObject jsonObject =null;

        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null){
            String result = EntityUtils.toString(entity,"UTF-8");
            jsonObject = JSONObject.fromObject(result);
        }
        return jsonObject;
    }
    /**
     * Post请求
     * @param url
     * @return
     */
    public static JSONObject doPostStr(String url,String outStr){
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonObject =null;
        try {
            httpPost.setEntity(new StringEntity(outStr,"UTF-8"));
            HttpResponse response = httpClient.execute(httpPost);
            String result = EntityUtils.toString(response.getEntity(),"UTF-8");
            jsonObject = jsonObject.fromObject(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    /**
     * 获取access_token
     * @return
     */
    public static AccessToken getAccessToken() throws Exception{
        AccessToken token = new AccessToken();
        String url = ACCESS_TOKEN_URL.replace("APPID",APPID).replace("APPSECRET",APPSECRET);
        JSONObject jsonObject = doGetStr(url);
        if (jsonObject != null){
            token.setToken(jsonObject.getString("access_token"));
            token.setExpiresIn(jsonObject.getInt("expires_in"));
        }
        return token;
    }
}

maven依赖

这里使用到了httpclient和JsonObject

    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.2.5</version>
    </dependency>

测试

package cn.notemi.test;

import cn.notemi.po.AccessToken;
import cn.notemi.util.WeixinUtil;

/**
 * Title:WeixinTest
 * Description:
 *
 * @author Flicker
 * @create 2017-07-26 下午 5:48
 **/
public class WeixinTest {
    public static void main(String[] args) throws Exception {
        AccessToken token = WeixinUtil.getAccessToken();
        System.out.println("Token:"+token.getToken());
        System.out.println("ExpiresIn:"+token.getExpiresIn());
    }
}

微信公众号开发 - 5.获取access_token

源代码

Weixin.rar