服务端API:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
获取小程序全局唯一后台接口调用凭据(access_token)。调调用绝大多数后台接口时都需使用 access_token,开发者需要进行妥善保存。
access_token 的存储与更新
access_token 的存储至少要保留 512 个字符空间;
access_token 的有效期目前为 2 个小时,需定时刷新,重复获取将导致上次获取的 access_token 失效;
建议开发者使用中控服务器统一获取和刷新 access_token,其他业务逻辑服务器所使用的 access_token 均来自于该中控服务器,不应该各自去刷新,否则容易造成冲突,导致 access_token 覆盖而影响业务;
access_token 的有效期通过返回的 expire_in 来传达,目前是7200秒之内的值,中控服务器需要根据这个有效时间提前去刷新。在刷新过程中,中控服务器可对外继续输出的老 access_token,此时公众平台后台会保证在5分钟内,新老 access_token 都可用,这保证了第三方业务的平滑过渡;
access_token 的有效时间可能会在未来有调整,所以中控服务器不仅需要内部定时主动刷新,还需要提供被动刷新 access_token 的接口,这样便于业务服务器在API调用获知 access_token 已超时的情况下,可以触发 access_token 的刷新流程。
参数配置Parameter
package com.austin.pay.common.base;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author liuqh
* @date 2019/6/10 下午2:35
* @description 系统参数
*/
@Component
@ConfigurationProperties(prefix = "parameter")
@Data
public class Parameter {
/**
* 小程序appid
*/
private String APPID;
/**
* 小程序appSecrect
*/
private String APP_SECRECT;
}
application.yml传入参数值
parameter:
a-p-p-i-d: 小程序appid
a-p-p-s-e-c-r-e-c-t: 小程序appSecrect
AccessToken实体
package com.austin.pay.common.weixin.token;
import lombok.Data;
/**
* @author liuqh
* @date 2019/8/6 下午1:51
* @description
*/
@Data
public class AccessToken {
private String access_token;
private Long expires_in;
}
GetToken发送请求,将得到的access_token和expires_in放进实体类
package com.austin.pay.common.weixin.token;
import com.austin.pay.common.weixin.utils.CommonUtil;
import com.austin.pay.common.weixin.utils.WeixinUrl;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author liuqh
* @date 2019/8/6 下午1:49
* @description
*/
@Component
public class GetToken {
private Logger logger = LoggerFactory.getLogger(GetToken.class);
public AccessToken getToken(String appid, String appSecrect) {
AccessToken token = null;
String url = WeixinUrl.ACCESS_TOKEN + "?grant_type=client_credential&appid=" + appid
+ "&secret=" + appSecrect;
String result = CommonUtil.httpsRequest(url, "GET", null);
JSONObject jsonObject = JSONObject.fromObject(result);
if (jsonObject != null) {
try {
token = new AccessToken();
token.setAccess_token(jsonObject.getString("access_token"));
token.setExpires_in(jsonObject.getLong("expires_in"));
} catch (Exception e) {
token = null;
e.printStackTrace();
logger.error("系统出错了!");
}
} else {
token = null;
// 获取token失败
logger.error("jsonObject为空,获取token失败");
}
return token;
}
}
设置内存字段token
package com.austin.pay.common.weixin.token;
/**
* @author liuqh
* @date 2019/8/6 下午2:30
* @description
*/
public class WeixinToken {
public static String token;
}
开启定时器AccessTokenTask将放进access_token内存,并设置延迟一秒执行,每7000秒调用一次
package com.austin.pay.common.weixin.token;
import com.austin.pay.common.base.Parameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author liuqh
* @date 2019/8/6 下午1:47
* @description
*/
@Component
public class AccessTokenTask {
private Logger logger = LoggerFactory.getLogger(AccessTokenTask.class);
@Autowired
private GetToken getToken;
@Autowired
private Parameter parameter;
/**
* access_token 是小程序的全局唯一调用凭据
* access_token 的有效期为 2 个小时,需要定时刷新 access_token,重复获取会导致之前一次获取的
* access_token 失效
* 延迟一秒执行
*/
@Scheduled(initialDelay = 1000, fixedDelay = 7000*1000 )
public void getTouTiaoAccessToken(){
try {
String token = getToken.getToken(parameter.getAPPID(), parameter.getAPP_SECRECT()).getAccess_token();
//将获取到的token放到内存
WeixinToken.token = token;
logger.info("获取到的微信accessToken为"+token);
} catch (Exception e) {
logger.error("获取微信adcessToken出错,信息如下");
e.printStackTrace();
}
}
}
主程序加入开启定时器的注解@EnableScheduling
package com.austin.pay;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class AustinPayApplication {
public static void main(String[] args) {
SpringApplication.run(AustinPayApplication.class, args);
}
}