微信支付java版本之获取Access_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"}
2.代码实现

APPID,APPSECRET在公众账号中可查询

package com.zhrd.bussinss.platform.scheduled;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zhrd.bussinss.platform.constants.WeiXinId;
import com.zhrd.bussinss.platform.service.AccessTokenService;

import net.sf.json.JSONObject;

@Component
@Lazy(false)
public  class GetWeiXinAccessTokenScheduled {
	
	/**

	    * 获得ACCESS_TOKEN

	    * 

	    * @Title: getAccess_token

	    * @Description: 获得ACCESS_TOKEN

	    * @param @return 设定文件

	    * @return String 返回类型

	    * @throws

	    */
	
		@Autowired
		private AccessTokenService accessTokenServiceImpl;
		
		
		@Scheduled(fixedRateString = "${weixin.token.fixedRate.in.milliseconds}"
				, initialDelayString = "${weixin.token.initialDelay.in.milliseconds}")
	   public void getAccessToken() {
			
			System.out.println("====================获取token开始==============================");

	       String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="

	               + WeiXinId.APPID+ "&secret=" + WeiXinId.APPSECRET;

	       String accessToken = null;
	       String expiresIn = null;

	       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 demoJson = JSONObject.fromObject(message);

	           accessToken = demoJson.getString("access_token");
	           expiresIn = demoJson.getString("expires_in");

	           System.out.println("accessToken===="+accessToken);
	           System.out.println("expiresIn==="+expiresIn);
	           accessTokenServiceImpl.addToken(accessToken,expiresIn);
	           System.out.println("====================获取token结束==============================");

	           is.close();

	       } catch (Exception e) {

	           e.printStackTrace();

	       }

//	       return accessToken;

	   }
	
}

3.其他接口


微信jsApi支付+发送模板消息: http://blog.csdn.net/wangxuewei111/article/details/43982381

微信支付native支付+工具类: http://blog.csdn.net/wangxuewei111/article/details/43954857






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值