发表于: 2017-12-21 23:56:57
1 604
今天完成的事情:完成微信接入接口
通过前端传入的code获取openid access_token
public String getOpenIdByCode(String code){
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WeChatConstant.AppID + "&secret=" + WeChatConstant.AppSecret + "&code=" + code + "&grant_type=authorization_code";
String message = this.surfingTheInternet(url);
log.info("message : " + message);
Gson gson = new Gson();
OpenIdByCode byCode = gson.fromJson(message,OpenIdByCode.class);
String access_token = byCode.getAccess_token();
String expires_in = byCode.getExpires_in();
String refresh_token = byCode.getRefresh_token();
String openid = byCode.getOpenid();
String scope = byCode.getScope();
log.info("byCode :" + byCode);
return openid;
}
public String surfingTheInternet(String url){
String message = null;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
//请求方法
http.setRequestMethod("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);
message = new String(jsonBytes, "UTF-8");
}catch (Exception e){
e.printStackTrace();
}
return message;
}
明天计划的事情:完成获取凭证 接口
遇到的问题:微信的接口新接触 不太了解 仿照的博韬的写的
像io流 http请求这些都不是很熟悉
公司框架还是不太熟 总是报一些错误 基本都是配置文件的问题
改了半天改好了
收获:初步学习了微信接口的调用
首先准备好 静态变量 appid AppSecret
新增了4个实体类 AccessToken OpenidByCode Person Token
里面的字段 基本都是微信的 东西 大部分只是一个中转
调用了很多微信的接口 请求
添加了很多微信的字段 很多 比较的杂乱
评论