还是要好好看看文档,弄明白哦
1.整一个测试公众号出来:你需要的东西都给你了:appID和appsecret
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
2.下载一个微信开发者工具,还是要扫一扫登录滴
3.开发
(1)建一个controller,记住你的controller上一层包名一定和配置文件一样!不然路径会报错,死活进不去断点(给自己看的)
(2)获取code,重要的红色圈起来了
@RequestMapping(value = "/auth", method = RequestMethod.GET)
public String auth(HttpServletRequest request){
String redirect_uri = "http://" + request.getServerName() + request.getContextPath() + "/weiXinAuth/userInfo"; //微信回调地址
String codeUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appID+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//GetRequest.sendGet(codeUrl);
return "redirect:" + codeUrl;
}
(3)用code获取access_token,注意有个坑:String code,String state是微信给你的,我刚开始没写state,一直报错。一句话,他给你的就得接着不管有没有用!! state主要是携带数据,你给微信传什么数据,再原封不动给你带回来
//获取token
@RequestMapping(value = "/userInfo",method = RequestMethod.GET)
public String userInfo(String code,String state,HttpServletRequest request) throws JSONException {
String tokenurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appID+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code";
String tokens=GetRequest.sendGet(tokenurl);//获取到access_token
if (tokens.indexOf("access_token")!=-1) {//校验token
JSONObject tokenJson = new JSONObject(tokens);
String access_token=tokenJson.getString("access_token");
String openid=tokenJson.getString("openid");
//(4)用access_token和openID获取用户基本信息
String userInfoUrl=" https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
String userInfo=GetRequest.sendGet(userInfoUrl);//获取到用户信息
if (tokens.indexOf("openid")!=-1) {
JSONObject json = new JSONObject(userInfo);//装换为json格式
//把openid保存到cookie中
// WeiXinInfo weiXinInfo = new WeiXinInfo();
// weiXinInfo.setOpenid(json.getString("openid"));
// weiXinInfo.setNickname(json.getString("nickname"));
// weiXinInfo.setSex(json.getString("sex"));
// weiXinInfo.setProvince(json.getString("province"));
// weiXinInfo.setCity(json.getString("city"));
// weiXinInfo.setCountry(json.getString("country"));
// weiXinInfo.setHeadimgurl(json.getString("headimgurl"));
// request.getSession().setAttribute("weiXinInfo",weiXinInfo);
return null;
}
}
工具:
package com.jzwl.jxjl.common.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* Created by admin on 2017/12/16.
*/
public class GetRequest {
/**
* 向指定URL发送GET方法的请求
* @param url 发送请求的URL
* @return URL所代表远程资源的响应
*/
public static String sendGet(String url){
String result = "";
BufferedReader in = null;
try
{
String urlName = url ;
URL realUrl = new URL(urlName);
//打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
//设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
//建立实际的连接
conn.connect();
//获取所有响应头字段
Map< String,List< String>> map = conn.getHeaderFields();
//遍历所有的响应头字段
for (String key : map.keySet()){
System.out.println(key + "--->" + map.get(key));
}
//定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine())!= null)
{
result += "\n" + line;
}
}
catch(Exception e){
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输入流
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return result;
}
}