慕课网access_token的获取(下)

接口调用返回的结果是两个字段:access_token和expires_in,所以需要创建一个实体类封装这两个字段

运行测试类WeixinTest.java控制台打印出错误信息

 重置开发者密钥试试看

还是报错。原来是把接口返回信息的字段名搞错了,是access_token不是access_tocken

再次运行测试类WeixinTest.java,可以了

可以可以,获取的时候先找本地文件里存的token,如果时间超过7200秒就重新获取,这个方法可以避免多次刷新产生冲突,人多了就麻烦了。这个可以自己课后去实现一下

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的刷新流程。


公众号可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在微信公众平台官网-开发页中获得(需要已经成为开发者,且帐号没有异常状态)。注意调用所有微信接口时均需使用https协议。如果第三方不使用中控服务器,而是选择各个业务逻辑点各自去刷新access_token,那么就可能会产生冲突,导致服务不稳定。

下面贴一下代码吧,与上一节重复的代码就不展示了,只展示本节主要的代码:

WeixinUtil.java

package com.imooc.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.imooc.po.AccessTocken;

import net.sf.json.JSONObject;

public class WeixinUtil {
private static final String APPID = "wxed6e9c3206e06192";
private static final String APPSECRET = "aa3f4c8ddced84c29ea0718d0b2f4a38";

private static final String ACCESS_TOCKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";//
//用到JSONObject,因为要把接收过来结果转成JSON格式
/**
* gett请求
* @param url
* @return
*/
public static JSONObject doGetStr(String url){//url为接口地址参数
//通过HttpClient去实现
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);//初始化的时候把url传递进去 它里面有一个HttpGet 因为我们这个方法是通过GET方式去提交
//所以我们用HttpGet的方式
JSONObject jsonObject = null;//创建一个接收变量
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity= response.getEntity();//有一个getEntity()方法从它的消息体里面去拿结果
if(entity!=null){
String result = EntityUtils.toString(entity,"UTF-8");//接收到的结果不为空,将entity转换成String类型
//可以将HttpEntity的类型转换成String的类型 设置编码格式 防止乱码
jsonObject = JSONObject.fromObject(result);//将String类型转换成JSON的格式 以便于后面以键值的方式去里面拿取结果
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//HttpResponse去接收它那个执行的结果
return jsonObject ;
}
/**
* post请求
* @param url
* @param outStr
* @return
*/
public static JSONObject doPostStr(String url,String outStr) throws ClientProtocolException, IOException{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = null;
try {
httpPost.setEntity(new StringEntity(outStr,"UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(),"UTF-8");//EntityUtils把接收到的结果转成String类型 接收的时候把结果转成UTF-8
jsonObject = JSONObject.fromObject(result);//把接收的结果转成JSON格式
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//
return null;
}
/**
* 获取access_tocken
* @return
*/
public static AccessTocken getAccessTocken(){
AccessTocken tocken = new AccessTocken();
String url = ACCESS_TOCKEN_URL.replace("APPID",APPID).replace("APPSECRET",APPSECRET);//把接口地址的两个实际参数给替换掉
JSONObject jsonObject = doGetStr(url);//通过doGet方法去获取它的结果
if(jsonObject!=null){
tocken.setTocken(jsonObject.getString("access_token"));//原来是把字段名搞错了卧槽
tocken.setExpiresIn(jsonObject.getInt("expires_in"));
}
return tocken;

}
}

AccessToken.java

package com.imooc.po;

public class AccessTocken {
private String tocken;//返回的凭证
private int expiresIn;//凭证的有效时间
public String getTocken() {
return tocken;
}
public void setTocken(String tocken) {
this.tocken = tocken;
}
public int getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(int expiresIn) {
this.expiresIn = expiresIn;
}


}

WeixinTest.java

package com.imooc.test;

import com.imooc.po.AccessTocken;
import com.imooc.util.WeixinUtil;

public class WeixinTest {
public static void main(String[] args) {
AccessTocken tocken = WeixinUtil.getAccessTocken();
System.out.println("票据:"+tocken.getTocken());
System.out.println("有效时间:"+tocken.getExpiresIn());
}
}

原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/6282151.html