记录一下自己写网页拉取微信登录授权的流程
1.首先需要在微信公众平台申请一个服务号
微信公众平台
2.在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;(在这里微信公众平台会生成一个校验文件,把校验文件放到项目的根目录下,即可通过审核)
3.通过微信文档可以得知拉取登陆授权需要四步
首先 根据链接用户可以在微信拉取微信授权 :(appID)https://open.weixin.qq.com/connect/oauth2/authorize?appid=appId&redirect_uri=redirect_uri&response_type=code&scope=snsapi_base&state=123#wechat_redirect
在用户点击同意授权之后,微信会回调之前的配置的网页回调地址;
我在请求微信成功之后返回了一个网页,用户扫码之后出来的就是这个网页
/**
* @author wx
* @version 创建时间:2020年4月8日 下午7:25:15
*
*/
public void callBack(String codeid ) throws IOException {
// 获取到授权标志code(用户同意微信授权之后产生的code)
hLog.info("进入微信回调"+"codeid= "+codeid);
String code = getRequest().getParameter("code");
// 通过code换取access_token
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WxAuthUtil.APP_ID + "&secret="
+ WxAuthUtil.APP_SECRET + "&code=" + code + "&grant_type=authorization_code";
JSONObject jsonObject = WxAuthUtil.doGetJson(url);
String openid = jsonObject.getString("openid");
String access_token = jsonObject.getString("access_token");
String refresh_token = jsonObject.getString("refresh_token");
// 校验access_token是否失效
String checkoutUrl = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + openid;
JSONObject checkoutInfo = WxAuthUtil.doGetJson(checkoutUrl);
// System.out.println("校验信息-----" + checkoutInfo.toString());
if (!"0".equals(checkoutInfo.getString("errcode")<