1 网页OAuth2.0授权获得用户信息(openId,头像,昵称等)
-
配置授权回调页面域名,域名不要带Http
-
用户授权并获取code
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
微信内访问此链接,会重定向到redirect_uri对应的链接地址,其中红色参数必填,绿色选填:
appid:公众号的唯一标识
redirect_uri:授权后重定向的回调链接地址
response_type:返回类型,请填写code
scope:snsapi_base(不弹出授权页面),snsapi_userinfo(弹出授权页面)
state:重定向后会带上state参数,开发者可以填写任意参数值
#wechat_redirect: 直接在微信打开链接,可以不填此参数。String code = request.getParameter("code");
- 使用code获取openId和access_token
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
后台访问此链接,其中:
appid:公众号的唯一标识
secret: 公众号的appsecret
code : 填写上一步获取的code参数
grant_type : 填写为authorization_code//发送网络请求 public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("Accept-Charset", "UTF-8"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; }
String url = "https://api.weixin.qq.com/sns/oauth2/access_token"; String param = "appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; String resultJson = sendGet(url, param);
可得到如下结果:
{
"access_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-G0ZWEE5-uBjBz941EOPqDQy5sS_GCs2z40dnvU99Y5AI1bw2uqN--2jXoBLIM5d6L9RImvm8Vg8cBAiLpWA8Vw",
"expires_in": 7200,
"refresh_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-G0ZWEE5-uBjBz941EOPqDQy5sS_GCs2z40dnvU99Y5CZPAwZksiuz_6x_TfkLoXLU7kdKM2232WDXB3Msuzq1A",
"openid": "oLVPpjqs9BhvzwPj5A-vTYAX3GLc",
"scope": "snsapi_userinfo,"
}
然后用Gosn解析出相关参数:Gson gs = new Gson(); Map<String,String> map = gs.fromJson(resultJson, Map.class); String openid = map.get("openid"); String access_token = map.get("access_token");
再根据这个获得用户名等信息:
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENIDString url = "https://api.weixin.qq.com/sns/userinfo"; String param = "access_token=ACCESS_TOKEN&openid=OPENID"; String resultJson = sendGet(url, param);
可得如下结果:
{
"openid": "oLVPpjqs9BhvzwPj5A-vTYAX3GLc",
"nickname": "刺猬宝宝",
"sex": 1,
"language": "简体中文",
"city": "深圳",
"province": "广东",
"country": "中国",
"headimgurl": "http://wx.qlogo.cn/mmopen/utpKYf69VAbCRDRlbUsPsdQN38DoibCkrU6SAMCSNx558eTaLVM8PyM6jlEGzOrH67hyZibIZPXu4BK1XNWzSXB3Cs4qpBBg18/0",
"privilege": []
}
然后再用Gson解析。