微信学习_06_access_token的获取

来源:互联网 发布:黄鸣龙 知乎 编辑:程序博客网 时间:2024/06/07 01:02

access_token官方文档

点击这里打开access_token官方文档

什么是access_token

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

接口调用请求说明

http请求方式: 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

类型转换,导入JAR包

这里因为需要把其他类型转成JSONObject,需要导入以下JAR包,详情可参见JSON与JavaBean之间互转
commons-beanutils-1.7.0.jar
commons-collections-3.2.1.jar
commons-lang-2.5.jar
commons-logging-1.0.4.jar
ezmorph-1.0.6.jar
json-lib-2.3-jdk15.jar

这里因为需要在后台发起请求获取response结果,所以需要用到DefaultHttpClient中的方法,需导进以下JAR包
httpclient-4.2.5.jar
httpcore-4.2.4

项目代码结构如下

项目代码结构

创建WeixinUtil

package com.imooc.util;import java.io.IOException;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.ParseException;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;import com.imooc.po.AccessToken;import net.sf.json.JSONObject;public class WeixinUtil {    private static final String APPID = "wxcd2e478a611d53cc";    private static final String APPSECRET = "8452eea12aad81a70eeb9470cfb2d751";    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     * @throws ParseException     * @throws IOException     */    public static JSONObject doGetStr(String url) throws ParseException, IOException{        DefaultHttpClient client = new DefaultHttpClient();        HttpGet httpGet = new HttpGet(url);        JSONObject jsonObject = null;        HttpResponse httpResponse = client.execute(httpGet);        HttpEntity entity = httpResponse.getEntity();        if(entity != null){            String result = EntityUtils.toString(entity,"UTF-8");            jsonObject = JSONObject.fromObject(result);        }        return jsonObject;    }    /**     * POST请求     * @param url     * @param outStr     * @return     * @throws ParseException     * @throws IOException     */    public static JSONObject doPostStr(String url,String outStr) throws ParseException, IOException{        DefaultHttpClient client = new DefaultHttpClient();        HttpPost httpost = new HttpPost(url);        JSONObject jsonObject = null;        httpost.setEntity(new StringEntity(outStr,"UTF-8"));        HttpResponse response = client.execute(httpost);        String result = EntityUtils.toString(response.getEntity(),"UTF-8");        jsonObject = JSONObject.fromObject(result);        return jsonObject;    }    /**     * 获取accessToken     * @return     * @throws ParseException     * @throws IOException     */    public static AccessToken getAccessToken() throws ParseException, IOException{        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;    }}

创建AccessToken实体类

package com.imooc.po;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;    }}

创建包com.imooc.test,在包中创建测试类WeixinTest

package com.imooc.test;import com.imooc.po.AccessToken;import com.imooc.util.WeixinUtil;public class WeixinTest {    public static void main(String[] args) {        try {            AccessToken token = WeixinUtil.getAccessToken();            System.out.println("票据"+token.getToken());            System.out.println("有效时间"+token.getExpiresIn());        } catch (Exception e) {            e.printStackTrace();        }    }}

测试

运行WeixinTest,得到票据和有效时间,测试通过

0 0