共计 3607 个字符,预计需要花费 10 分钟才能阅读完成。
1. 网页授权回调域名
在微信公众号请求用户网页授权之前,需要先到公众平台官网中,修改授权回调域名,请注意,这里填写的是域名(是一个字符串),而不是 URL,因此请勿加 http:// 等协议头
2. 网页授权的两种 scope 的区别
1)以 snsapi_base 为 scope 发起的网页授权,是用来获取进入页面的用户的 openid 的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)
2)以 snsapi_userinfo 为 scope 发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。
3. 网页授权流程分为四步
1)引导用户进入授权页面同意授权,获取 code
// 前端发请请求 | |
created() {this.axios.get('/wx/get_code?url=' + this.shareUrl).then((res) => {if (res.code == 0) {window.location.href = res.data;} | |
}).catch((error) => {console.log(error) | |
}); | |
} | |
/** | |
* 1. 用户同意授权,获取 code | |
*/ | |
@RequestMapping(value = "/get_code", method = RequestMethod.GET) | |
public String getWxCode() throws UnsupportedEncodingException { | |
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Constants.APPID + "&redirect_uri=" | |
+ URLEncoder.encode("http://192.168.0.152:8080/get_auth_access_token", "UTF-8") + "&response_type=code&scope=" | |
+ Constants.GRANTSCOPE + "&state=STATE#wechat_redirect"; | |
} |
scope 等于 snsapi_userinfo 时的授权页面
2)通过 code 换取网页授权 access_token(与基础支持中的 access_token 不同)
/** | |
* 2. 通过 code 换取网页授权 access_token | |
*/ | |
@RequestMapping(value = "/get_auth_access_token", method = RequestMethod.GET) | |
public void getAuthAccessToken(HttpServletRequest request, HttpServletResponse response, String code) throws IOException { | |
String urlToken = "https://api.weixin.qq.com/sns/oauth2/access_token"; | |
String accessTokenObj = HttpClientUtil.sendGet(urlToken, "appid=" + Constants.APPID + "&secret=" | |
+ Constants.APPSECRET + "&code=" + code + "&grant_type=authorization_code"); | |
JSONObject jsonToken = JSONObject.fromObject(accessTokenObj); | |
String openId = null; | |
String accessToken = null; | |
if (StringUtils.isNotBlank(String.valueOf(jsonToken))) {openId = jsonToken.getString("openid"); | |
accessToken = jsonToken.getString("access_token"); | |
} | |
logger.info("openid={},access_token={}", openId, accessToken); | |
} | |
} |
3)如果需要,开发者可以刷新网页授权 access_token,避免过期
由于 access_token 拥有较短的有效期,当 access_token 超时后,可以使用 refresh_token 进行刷新,refresh_token 有效期为 30 天,当 refresh_token 失效之后,需要用户重新授权。
4)通过网页授权 access_token 和 openid 获取用户基本信息(支持 UnionID 机制)
/** | |
* 2. 通过 code 换取网页授权 access_token | |
*/ | |
@RequestMapping(value = "/get_auth_access_token", method = RequestMethod.GET) | |
public void getAuthAccessToken(HttpServletRequest request, HttpServletResponse response, String code) throws IOException { | |
String urlToken = "https://api.weixin.qq.com/sns/oauth2/access_token"; | |
String accessTokenObj = HttpClientUtil.sendGet(urlToken, "appid=" + Constants.APPID + "&secret=" | |
+ Constants.APPSECRET + "&code=" + code + "&grant_type=authorization_code"); | |
JSONObject jsonToken = JSONObject.fromObject(accessTokenObj); | |
String openId = null; | |
String accessToken = null; | |
if (StringUtils.isNotBlank(String.valueOf(jsonToken))) {openId = jsonToken.getString("openid"); | |
accessToken = jsonToken.getString("access_token"); | |
} | |
logger.info("openid={},access_token={}", openId, accessToken); | |
//3. 拉取用户信息(需 scope 为 snsapi_userinfo) | |
String urlInfo = "https://api.weixin.qq.com/sns/userinfo"; | |
String infoObj = HttpClientUtil.sendGet(urlInfo, "access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN"); | |
JSONObject jsonUserInfo = JSONObject.fromObject(infoObj); | |
if (StringUtils.isNotBlank(String.valueOf(jsonUserInfo))) {String nickName = jsonUserInfo.getString("nickname"); | |
String headImgUrl = jsonUserInfo.getString("headimgurl"); | |
String sex = jsonUserInfo.getString("sex"); | |
response.sendRedirect("http://lyx1314520ll.w3.luyouxia.net/auth?nickName=" + nickName + "&headImgUrl=" + headImgUrl + "&sex=" + sex); | |
} | |
} |
unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
公众号通过微信网页授权机制后,重定向页面
4. 网页授权遇到的问题
1)redirect_uri 参数错误
测试微信公众号回调地址支持域名和 ip,正式公众号回调地址只支持域名,不要加 https://,http:// 这些前缀,如:www.baidu.com。
edirect_uri 可以是你网站下的任何页面(不局限于授权回调域配置的域名),但是一定要在前面加上 http://,并且使用 urlencode 编码。
2)该链接无法访问,code:-1
code 作为换取 access_token 的票据,每次用户授权带上的 code 将不一样,code 只能使用一次,5 分钟未被使用自动过期,重新获取就好
想要了解更多内容可以阅读微信开发者文档,里面详情的讲解了这些内容 https://mp.weixin.qq.com/wiki…