标签:java 微信开发 access_token
为了获得长期有效的access_token,不能每次都从服务器生成,本文采用存储在文件中,超过一定时间再定时刷新的方法,直接看代码。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
/*
* 该类测试了直接获取Access_token和保存后判断Access_token存活时间后再获取两种方式【使用时,使用该方式】。
*/
public class Access_tokenDemo {
private static final long MAX_TIME = 7200 * 1000;// 微信允许最长Access_token有效时间(ms)
private static final String TAG = "WeixinApiTest";// TAG
private static final String APPID = "your appId";// APPID
private static final String SECERT = "your appSecret";// 秘钥
/*
* 该方法实现获取Access_token、保存并且只保存2小时Access_token。如果超过两个小时重新获取;如果没有超过两个小时,直接获取。该方法依赖
* :public static String getAccessToken();
*
* 思路:将获取到的Access_token和当前时间存储到file里,
* 取出时判断当前时间和存储里面的记录的时间的时间差,如果大于MAX_TIME,重新获取,并且将获取到的存储到file替换原来的内容
* ,如果小于MAX_TIME,直接获取。
*/
public String getSavedAccess_token() {
Gson gson = new Gson();
String mAccess_token = null;// 需要获取的Access_token;
File file = new File("temp_access_token.temp");// Access_token保存的位置
try {
// 如果文件不存在,创建
if (!file.exists())
file.createNewFile();
// 如果文件大小等于0,说明第一次使用,存入Access_token
if (file.length() == 0) {
mAccess_token = getAccessToken();
FileOutputStream fos = new FileOutputStream(file, false);// 不允许追加
Access_token at = new Access_token();
at.setAccess_token(mAccess_token);
at.setExpires_in(System.currentTimeMillis() + "");
String json = gson.toJson(at);
fos.write((json).getBytes());
fos.close();
} else {
// 读取文件内容
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[2048];
int len = fis.read(b);
String mJsonAccess_token = new String(b, 0, len);// 读取到的文件内容
Access_token access_token = gson.fromJson(mJsonAccess_token,
new Access_token().getClass());
if (access_token.getExpires_in() != null) {
long saveTime = Long.parseLong(access_token.getExpires_in());
System.out.println("saveTime:"+saveTime);
long nowTime = System.currentTimeMillis();
System.out.println("nowTime:"+nowTime);
long remianTime = nowTime - saveTime;
System.out.println("时间差:" +remianTime);
// System.out.println(TAG + "时间差:" + remianTime);
if (remianTime < MAX_TIME) {
Access_token at = gson.fromJson(mJsonAccess_token,
new Access_token().getClass());
mAccess_token = at.getAccess_token();
} else {
mAccess_token = getAccessToken();
FileOutputStream fos = new FileOutputStream(file, false);// 不允许追加
Access_token at = new Access_token();
at.setAccess_token(mAccess_token);
at.setExpires_in(System.currentTimeMillis() + "");
String json = gson.toJson(at);
fos.write((json).getBytes());
fos.close();
}
}
}
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("获取到的Access_token是:" + mAccess_token);
return mAccess_token;
}
/*
* 获取微信服务器AccessToken。该部分和getAccess_token() 一致,不再加注释
*/
public static String getAccessToken() {
String urlString = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ APPID + "&secret=" + SECERT;
String reslut = null;
try {
URL reqURL = new URL(urlString);
HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL
.openConnection();
InputStreamReader isr = new InputStreamReader(
httpsConn.getInputStream());
char[] chars = new char[1024];
reslut = "";
int len;
while ((len = isr.read(chars)) != -1) {
reslut += new String(chars, 0, len);
}
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
Access_token access_token = gson.fromJson(reslut,
new Access_token().getClass());
if (access_token.getAccess_token() != null) {
return access_token.getAccess_token();
} else {
return null;
}
}
}
本文参考了高远的博客http://blog.csdn.net/wgyscsf/article/details/51078372
,并略作了修改。
标签:java 微信开发 access_token
原文地址:http://11434266.blog.51cto.com/11424266/1907628