微信官方文档html
准备工做前端
在进行第三方受权登陆以前,须要在微信开放平台注册开发者帐号,拿到相应的AppId和AppSecret以及redirect_uri,便可进行受权接入流程java
受权流程说明 总体流程分:json
1. 第三方发起微信受权登陆请求,微信用户容许受权第三方应用后,微信会拉起应用或重定向到第三方网站,而且带上受权临时票据code参数;
2. 经过code参数加上AppID和AppSecret等,经过API换取access_token;
3. 经过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操做。
复制代码
第一步:请求code 根据参数访问连接获取受权信息 用户容许受权后,将会重定向到redirect_uri的网址上,而且带上code和state参数; 若用户禁止受权,则重定向后不会带上code参数,仅会带上state参数 api
String oauthUrl = "https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
String redirect_uri = URLEncoder.encode(WXConfig.CALLBACK, "utf-8");
oauthUrl = oauthUrl.replace("APPID", WXConfig.PC_JSID).replace("REDIRECT_URI", redirect_uri).replace("SCOPE", WXConfig.SCOPE);
model.addAttribute("oauthUrl", oauthUrl);
model.addAttribute("appid", WXConfig.PC_JSID);
model.addAttribute("scope", WXConfig.SCOPE);
model.addAttribute("redirect_uri", redirect_uri);
复制代码
前端经过回调连接生成二维码: 第二步:经过code获取access_tokeen微信
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
复制代码
第三步:经过access_token调用接口 获取access_token后,进行接口调用,有如下前提:markdown
1. access_token有效且未超时;
2. 微信用户已受权给第三方应用账号相应接口做用域(scope)。
复制代码
获取用户我的信息:app
受权做用域(scope) | 接口 |
---|---|
snsapi_userinfo | /sns/userinfo |
参考代码: |
public Map<String, String> pcCode2Session(String js_code) throws UnsupportedEncodingException {
//1.经过code获取access_token
String url = WXConstants.DOMAIN_API + WXConstants.OAUTH2_URL_SUFFIX
+ "?appid=" + WXConfig.PC_JSID + "&secret="
+ WXConfig.PC_JSSECRET + "&code="
+ js_code + "&grant_type=authorization_code";
String result = restTemplate.getForObject(url, String.class);
if (result != null && result.trim().length() != 0) {
JSONObject json = JSONObject.parseObject(result);
Integer code = json.getInteger("errcode");
if (code == null || code == WXConstants.SUCCESS) {
//获取access_token
Map<String, String> map = new HashMap<>();
map.put("access_token", json.getString("access_token"));
map.put("expires_in", json.getString("expires_in"));
map.put("refresh_token", json.getString("refresh_token"));
map.put("openid", json.getString("openid"));
map.put("scope", json.getString("scope"));
map.put("unionid", json.getString("unionid"));
//经过access_token和openid获取用户我的信息(头像、昵称)
String url_user = WXConstants.DOMAIN_API + WXConstants.USERINFO_URL_SUFFIX
+ "?access_token=" + json.getString("access_token") + "&openid="
+ json.getString("openid");
String result_user = restTemplate.getForObject(url_user, String.class);
result_user = new String(result_user.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("--------result2="+result_user);
if (result_user != null && result_user.trim().length() != 0) {
JSONObject json_user = JSONObject.parseObject(result_user);
Integer code_user = json_user.getInteger("errcode");
System.out.println("======="+code_user);
if (code_user == null || code_user == WXConstants.SUCCESS) {
//用户头像url
map.put("headimgurl", json_user.getString("headimgurl"));
//用户昵称
map.put("nickname", json_user.getString("nickname"));
}
}
return map;
} else {
LOGGER.error("微信JSAPI请求失败:{}", result);
}
} else {
LOGGER.error("微信JSAPI请求失败:null");
}
return null;
}
复制代码
注:未调用刷新access_token有效期,由于项目中已经作了超时处理 oop