微信公众号开发----Java------ 通过密钥获取accessToken 和 expiresIn

本文介绍了一种通过Java实现的微信API调用方法,详细展示了如何定义常量实体类、响应参数实体类以及封装HTTP请求接口来获取微信Access Token的过程。通过正确的密钥配置,实现了对微信服务器的有效调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.定义常量实体类  

//class WechatConfig

/**
 * 微信配置类一
 */
public class WechatConfig {
    public static final String appID = "xxxxxxxxx";
    public static final String appsecret = "xxxxxxx";
}

2定义响应/参数实体类  

//此处使用了lombok 驱动包,用来省写set和get

@Data
public class AccessTokenEntity {

    private String accessToken;
    private int expiresIn;

}

3.封装接口 

    public static AccessTokenEntity getAccessTokenAndTime() throws IOException {
        //使用默认配置创建httpclient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //配置超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000).setConnectionRequestTimeout(5000)
                .setSocketTimeout(5000).setRedirectsEnabled(true)
                .build();
        //声明url字符串变量,并复制
        String oldurl ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        //替换字符串的两个值
        String newurl = oldurl.replace("APPID", WechatConfig.appID).replace("APPSECRET",WechatConfig.appsecret).trim();
        //http请求,并装配url地址
        HttpGet httpGet = new HttpGet(newurl);
        //http请求,装配配置超时时间
        httpGet.setConfig(requestConfig);
        try {
            //httpclient执行请求,返回响应
            HttpResponse response = httpClient.execute(httpGet);
            //声明响应变量字符串
            String result = "";
            //用string接受响应实体内容
            result = EntityUtils.toString(response.getEntity(),"utf-8");
            //内容里带有下划线的变量全部替换成小驼峰命名
            String newResult = result.replace("access_token","accessToken").replace("expires_in","expiresIn");
            //string 转jsonObject
            JSONObject jsonObject = JSONObject.fromObject(newResult);
            //jsonObject 转 javaBean
            AccessTokenEntity accessTokenEntity = (AccessTokenEntity) JSONObject.toBean(jsonObject ,AccessTokenEntity.class);
            return accessTokenEntity;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            httpClient.close();
        }
        return null;
    }

只要正确配置密钥,最后测试,,即可看到效果。

 

 

需要用到的maven驱动包 

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值