java-微信公众号获取微信access_token

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013008827/article/details/89085133

什么是access_token

调用access_token请求说明

代码实现

目录

什么是access_token

微信公众平台技术文档中对access_token的解释:

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

技术文档中建议

1.使用中控服务器统一获取刷新access_token,避免不同的业务逻辑各自刷新容易引起冲突;

2.access_token中有expire_in,目前是7200秒之内的值可以根据该值控制刷新access_token,刷新过程中中控服务器可以继续对外输出老的access_token,公众平台后台会保证在5分钟内新老access_token可以继续使用;

调用access_token请求说明

使用https请求:GET

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

请求成功后返回JSON数据包

{"access_token":"ACCESS_TOKEN","expires_in":7200}

错误时会返回错误原因

{"errcode":40013,"errmsg":"invalid appid"}

代码实现

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package priv.liu.weichat.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;

import net.sf.json.JSONObject;

/**
 *
 * @author liu
 */
public class HttpsUtil {    
    /**
     * HttpsUtil方法https请求结果返回蔚json类型
     * @param Url http请求地址
     * @param Method http请求类型支持POST GET
     * @param Output
     * @return InputStream转换成JSONObject后返回
     * @throws Exception 
     */
    public JSONObject HttpsUtil(String Url,String Method,String Output) throws Exception{
        JSONObject jsonObject = null;
        URL conn_url =  new URL(Url);
        HttpURLConnection conn = (HttpsURLConnection)conn_url.openConnection();
        conn.setRequestMethod(Method);
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.connect();
        //output获取access_token是不会用到
        if(Output != null){
            OutputStream  outputstream =conn.getOutputStream();
            //字符集,防止出现中文乱码
            outputstream.write(Output.getBytes("UTF-8"));
            outputstream.close();
        }
        //正常返回代码为200
        if(conn.getResponseCode()==200){
            InputStream stream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
        stream.close();
        conn.disconnect();
            jsonObject = JSONObject.fromObject(buffer.toString());
        }
        System.out.println(conn.getResponseCode());
        return jsonObject;
    }   
}
返回结果:
{
    "access_token": "20_CSrzfxz8AEWg10L8BkeXj8bJfc4umpiNWnjev67oUFJWYAR-91ORgXqPHQf5EZrfauKOhH9-Gb0fd4Ev1Viz-nTkwQPl6Q0c1aV6-GVAFVL1OVnEmEa3aifjGfbvtUkrUOwmAWceT1xkkiQwGTLfAEAYLD", 
    "expires_in": 7200
}
参数 说明
access_token 获取到的凭证
expires_in 凭证有效时间,单位:秒

获取到的access_token有效期只有两个小时,这块建议做个定时任务


猜你喜欢

转载自blog.csdn.net/u013008827/article/details/89085133