Springboot开发微信公众号(三)

Access_token

一、Access_token
access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_toke。access_token的有效时间时2个小时,并且调用的次数是有限的,需要定时获取,并配置全局。通过访问接口的方式获取access_token.在之后的开发中会很常见,调用接口。
1.添加httpclient处理

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

2.定时获取



import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;

import javax.annotation.PostConstruct;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 成小新
 * @ProjectName wechat
 * @Description: 定时器
 * @date 2018/11/13:38 PM
 * @email zhaoboy9692@163.com
 */
@Component
@EnableScheduling
public class Scheduler {


    private static String access_token;

	//读取appid和appsecret
    @Autowired
    private ReadApplicationUntil readApplicationUntil;

    private static ReadApplicationUntil readApplicationUntils;

    @PostConstruct
    public void init() {
        readApplicationUntils = readApplicationUntil;
    }

    /**
     * 定时获取access_token
     *
     * @throws SQLException
     */
    @Scheduled(fixedDelay = 100)
    public void setAccessToken() {
        String grant_type = "client_credential";
        String AppId = readApplicationUntils.getAppid();
        String secret = readApplicationUntils.getAppsecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + AppId + "&secret=" + secret;
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JsonObject returnData = new JsonParser().parse(message).getAsJsonObject();
            access_token = returnData.get("access_token").getAsString();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getAccessToken() {
        return access_token;
    }

}

3.读取yml配置文件工具类



import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author 成小新
 * @Title: ReadApplicationUNtil
 * @ProjectName wechat
 * @Description: 获取yml配置文件的属性值
 * @date 2018/10/303:36 PM
 * @email zhaoboy9692@163.com
 */
@Component
//接收application.yml中的wechat下面的属性
@ConfigurationProperties(prefix = "wechat")
public class ReadApplicationUntil {
    private String token;
    private String appid;
    private String appsecret;

    public String getToken() {
        return token;
    }

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

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getAppsecret() {
        return appsecret;
    }

    public void setAppsecret(String appsecret) {
        this.appsecret = appsecret;
    }
}

4.测试类



import com.xbjs.wechat.Utils.Scheduler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class WechatApplicationTests {
    @Autowired
    private Scheduler scheduler;




    @Test
    public void token() {
        scheduler.setAccessToken();
        System.out.println(scheduler.getAccessToken());
        System.out.println("--------------");


    }

}
看图

在这里插入图片描述
希望能交流更多技术,关注小白的微信公众号吧。在这里插入图片描述

小白技术社
原文地址:https://www.cnblogs.com/xbjss/p/13326697.html