发表于: 2017-11-19 22:53:58
1 590
今天完成的事情:
完成了微信公众号自定义菜单跳转并获得微信信息的功能
微信公众号获取各种返回参数,如access_token、openid、乃至微信用户名,微信用户头像的的方法为:
使用开发文档中提供的url,例如获取access_token,其提供的网址为:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的 appID&secret=你的app密匙
这个我的测试号使用的url
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxaa8cc3e512810e0c&secret=f526453358bf5ea725985d9bdfd38b5d
键入浏览器:
返回了access_token,和其有效时间7200s
使用代码的方式获取:
public static String getAccessToken() throws IOException {
String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid=wxaa8cc3e512810e0c&secret=f526453358bf5ea725985d9bdfd38b5d";
URL url = new URL(accessTokenUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
int size = inputStream.available();
byte[] bs = new byte[size];
inputStream.read(bs);
String message = new String(bs, "UTF-8");
JSONObject jsonObject = new JSONObject(message);
return jsonObject.getString("access_token");
}
然后是创建微信自定义菜单:
public static String setMenu() {
JSONObject firstLevelMenu = new JSONObject();
JSONArray firstLevelArray = new JSONArray();
//一级菜单
JSONObject firstLevelMenuContext1 = new JSONObject();
firstLevelMenuContext1.put("type", "view");
firstLevelMenuContext1.put("name", "跳转登录");
firstLevelMenuContext1.put("url", "http://www.summerwaves.cn/redirect");
firstLevelArray.put(firstLevelMenuContext1);
firstLevelMenu.put("button", firstLevelArray);
return firstLevelMenu.toString();
}
生成json
结果:
{"button":[{"name":"跳转登录","type":"view","url":"http://www.summerwaves.cn/redirect"}]}
这个不经常使用,所以就不使用代码生成自定义菜单了,直接使用接口调试工具
地址:https://mp.weixin.qq.com/debug
手机上的效果,点击“跳转登录”会跳转到我们在生成JSON中设置的url,即
http://www.summerwaves.cn/redirect
然后是获取用户信息的内容:
跳转到http://www.summerwaves.cn/redirect
@RequestMapping(value = "/redirect")
public String redirect() {
return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaa8cc3e512810e0c&redirect_uri=http://www.summerwaves.cn/oauth?response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect";
}
然后是重定向至微信公众号提供的一个超长的网址
参数说明:
重定向至http://www.summerwaves.cn/oauth
@RequestMapping(value = "/oauth", method = RequestMethod.GET)
public ModelAndView weixinOAuth(HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelAndView mv = new ModelAndView();
mv.setViewName("message");
//得到code
String CODE = request.getParameter("code");
String openid = WeChatUtil.getOpenid(CODE);
wcUser wcUser = WeChatUtil.getUserMessage(openid);
mv.addObject("openid", openid);
mv.addObject("wcUser", wcUser);
return mv;
}
在这一步,我们获取到微信端返回的一个“code”参数,而这个参数可以获取到用户的openid,而通过openid可以获取用户的各种信息,如用户名,用户头像,用户所在地等
使用code获取用户openid:
public static String getOpenid(String code) throws IOException {
//换取access_token 其中包含了openid
String URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code".replace("APPID", APPID).replace("SECRET", SECRET).replace("CODE", code);
java.net.URL url = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
int size = inputStream.available();
byte[] bs = new byte[size];
inputStream.read(bs);
String jsonStr = new String(bs, "UTF-8");
JSONObject jsonObj = new JSONObject(jsonStr);
//有了用户的opendi就可以的到用户的信息了
//地址为https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
//得到用户信息之后返回到一个页面
return jsonObj.get("openid").toString();
}
使用openid获取用户信息:
public static wcUser getUserMessage(String openid) throws IOException {
String URL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN".replace("ACCESS_TOKEN", getAccessToken()).replace("OPENID", openid);
java.net.URL url = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
int size = inputStream.available();
byte[] bs = new byte[size];
inputStream.read(bs);
String jsonStr = new String(bs, "UTF-8");
//System.out.println(jsonStr);
//out.print(jsonStr);
JSONObject jsonObj = new JSONObject(jsonStr);
wcUser wcUser = new wcUser();
wcUser.setNickname(jsonObj.getString("nickname"));
wcUser.setAvatar(jsonObj.getString("headimgurl"));
logger.info("nickname:" + wcUser.getNickname() + ",avatar:" + wcUser.getAvatar());
return wcUser;
}
如代码所示,获取到了用户名和用户头像
太长不看总结:
设置获取access_token的工具类——》设置自定义菜单——》通过自定义菜单重定向至获取“code”参数网址——》通过“code”参数获取openid——》通过openid获取用户信息
而这个过程是不能在微信外的浏览器浏览的,所以会出现一个错觉:这是微信端的东西
经过这两天的搭建,发现这个是完完全全的web项目,不过只能通过微信公众号登陆,获取其用户信息而已
测试地址:
资料整理:
微信端口测试号申请:
网页授权:
自定义菜单的开发:
获取用户信息:
微信开发文档:
明天计划的事情:
了解下搭建环境的事情,评审近了,准备一下
遇到的问题:
功能还不完全,如何判断获取的access_token是否过期,放在哪里,这都是一个问题,不过这些可以放在项目搭建完成之后再搞,不会耗费很多时间
收获:
学习到了微信公众号的各种设置的方法,以及如何通过自定义菜单跳转获取用户参数
进度:
微信端处理用户信息已完成,准备进行环境搭建
评论