码迷,mamicode.com
首页 > 微信 > 详细

【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永不过期

时间:2015-08-19 16:50:28      阅读:7240      评论:0      收藏:0      [点我收藏+]

标签:微信获取token   以及储存token方法   spring quartz让token永   

官网说明

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

公众平台的API调用所需的access_token的使用及生成方式说明:

1、为了保密appsecrect,第三方需要一个access_token获取和刷新的中控服务器。而其他业务逻辑服务器所使用的access_token均来自于该中控服务器,不应该各自去刷新,否则会造成access_token覆盖而影响业务;
2、目前access_token的有效期通过返回的expire_in来传达,目前是7200秒之内的值。中控服务器需要根据这个有效时间提前去刷新新access_token。在刷新过程中,中控服务器对外输出的依然是老access_token,此时公众平台后台会保证在刷新短时间内,新老access_token都可用,这保证了第三方业务的平滑过渡;
3、access_token的有效时间可能会在未来有调整,所以中控服务器不仅需要内部定时主动刷新,还需要提供被动刷新access_token的接口,这样便于业务服务器在API调用获知access_token已超时的情况下,可以触发access_token的刷新流程。

如果第三方不使用中控服务器,而是选择各个业务逻辑点各自去刷新access_token,那么就可能会产生冲突,导致服务不稳定。

公众号可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在微信公众平台官网-开发者中心页中获得(需要已经成为开发者,且帐号没有异常状态)。注意调用所有微信接口时均需使用https协议。

接口调用请求说明

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

返回说明

正常情况下,微信会返回下述JSON数据包给公众号:

{"access_token":"ACCESS_TOKEN","expires_in":7200}
参数 说明
access_token 获取到的凭证
expires_in 凭证有效时间,单位:秒


错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):

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

个人实现.

公众号给用户发消息,用的是基础token,而token,每日上线10W次,如果用户量比较大带根本不够用,节约资源,将token储存起来,在实际开发中,并不能把APPID,SECRET写死,我们将保存在数据配置表中,所以看代码吧!

1.微信工具类定义获取token方法.

	public static GeneralToken getToken(String appid, String secret){
		GeneralToken gt = null;
		try {
			HttpClient hc = new HttpClient();
			Map<String, String> params = new HashMap<String, String>();
			params.put("appid", appid);
			params.put("secret", secret);
			params.put("grant_type", "client_credential");
			String url = "https://api.weixin.qq.com/cgi-bin/token";
			gt =  hc.post(url, params, new JsonParser<GeneralToken>(GeneralToken.class));
		} catch (IOException e) {
			log.error("get token error message:" + e.getMessage() , e); 
			e.printStackTrace();
		}
		return gt;
	}

	public class GeneralToken {
		private String expires_in; //成功有效时间
		private String access_token;  // 普通Token
		private String errcode; //失败ID
		private String errmsg; //失败消息
		//get set 忽略
	}

2.自定义单例类(单例类一个实例,并且实例化由类带本身完成,并向其它类提供使用)

public class Token {
	private Token() {}
	private  String token;
	private static Token instance  = new Token();
	public static Token getInstance() {
		return instance;
	}
	public  String getToken() {
		return token;
	}
	public  void setToken(String token) {
		this.token = token;
	}
}

3.给Token类中token变量赋值,

 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期。当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件,该事件由ServletContextListener 来处理。在 ServletContextListener 接口中定义了处理ServletContextEvent事件的两个方法。

方法1:contextInitialized(ServletContextEvent sce) :当Servlet 容器启动Web 应用时调用该方法。在调用完该方法之后,容器再对Filter 初始化,并且对那些在Web 应用启动时就需要被初始化的Servlet进行初始化。
方法2:contextDestroyed(ServletContextEvent sce) :当Servlet 容器终止Web 应用时调用该方法。在调用该方法之前,容器会先销毁所有的Servlet 和Filter 过滤器。


public class ContextListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		ConfigurationService configurationService = ServerUtil.getBean(ConfigurationService.class);
		String appid = configurationService.getValue(Constants.WX_APPID, Constants.CONFIGURATION_GROUP_WEIXIN);
		String secret = configurationService.getValue(Constants.WX_APPSECRET, Constants.CONFIGURATION_GROUP_WEIXIN);
		GeneralToken gt = WeixinMessageUtil.getToken(appid, secret);
		if(gt != null && !StringUtil.isNullOrEmpty(gt.getAccess_token())){
			Token.getInstance().setToken(gt.getAccess_token());
		}
	}
}

4.使用Spring quartz让Token永不过期。

applicationContext.xml,添加配置。
下面定义定时器,每两个小时刷新Token。

    <bean id="getTokenTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<property name="targetObject">
    			<ref bean="taskService" />
    		</property>
    		<property name="targetMethod">
    			<value>getToken</value>
    		</property>
    		<property name="concurrent" value="true" />
    </bean>
    <bean id="getTokenTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
    	<property name="jobDetail">
    		<ref bean="getTokenTask"/>
    	</property>
    	<property name="cronExpression">
    		<value>0 0 0/2 * * ?</value>
    	</property>
    </bean>
    <bean id="startQuertz" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    	<property name="triggers">
    		<list>
    			<ref bean="getTokenTime"/>
    		</list>
    	</property>
    </bean>
添加创建TaskService接口
public interface TaskService {
	public void getToken();
}
实现TaskService接口
@Service("taskService")
public class TaskServiceImpl implements TaskService {
	private static Token tokenCache = Token.getInstance();
	@Override
	public void getToken() {
		try {
			HttpClient hc = new HttpClient();
			if (!StringUtil.isNullOrEmpty(Constants.WX_APPID) && !StringUtil.isNullOrEmpty(Constants.WX_APPSECRET)) {
				Map<String, String> params = new HashMap<String, String>();
				params.put("appid", Constants.WX_APPID);
				params.put("secret", Constants.WX_APPSECRET);
				params.put("grant_type", "client_credential");
				String url = "https://api.weixin.qq.com/cgi-bin/token";
				GeneralToken gt = hc.post(url, params, new JsonParser<GeneralToken>(GeneralToken.class));
				if (gt != null && !StringUtil.isNullOrEmpty(gt.getAccess_token())) {
					System.out.println("token:" +gt.getAccess_token());
					tokenCache.setToken(gt.getAccess_token());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

注:其中HttpClient 类是被封装过的工具类,

版权声明:本文为博主原创文章,未经博主允许不得转载。

【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永不过期

标签:微信获取token   以及储存token方法   spring quartz让token永   

原文地址:http://blog.csdn.net/inforstack/article/details/47781101

(3)
(2)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!