代码拉取完成,页面将自动刷新
@Controller
@RequestMapping("/weixin")
public class WeixinOauth {
/*
* 第一:用户同意授权,获取 code
* */
@RequestMapping("/oauth")
public void oauth(HttpServletResponse response) throws IOException {
// 转发到获取access_token、openid方法
// 例如:localhost:8080/项目名/getAccessToken
redirectUri = URLEncoder.encode(redirectUri,"utf8");
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
"appid=" + WEIXIN_APPID +
"&redirect_uri=" + redirectUri +
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=STATE" +
"#wechat_redirect";
response.sendRedirect(url);
}
/*
* 根据 code 获取 access_token openid
* */
@RequestMapping("/getAccessToken")
public String getAccessToken(HttpServletRequest request,HttpServletResponse response) throws IOException {
String code = request.getParameter("code");
//第二: 认证服务带着 code发送请求,得到 access_token openid
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=" + FINEMP_WEIXIN_APPID +
"&secret=" + FINEMP_WEIXIN_APPSECRET +
"&code=" + code +
"&grant_type=authorization_code";
JSONObject jsonObject = HttpClientUtils.doGet(url);
String weixin_accessToken = jsonObject.getString("access_token");
String weixin_openid = jsonObject.getString("openid");
logger.info("jsonObject=====================:{}"+ jsonObject);
// 第三:带着 openid 和 access_token 获取你的微信账号资源信息
String urlUserInfo = "https://api.weixin.qq.com/sns/userinfo?" +
"access_token=" + weixin_accessToken +
"&openid=" + weixin_openid +
"&lang=zh_CN";
JSONObject userObject = HttpClientUtils.doGet(urlUserInfo);
logger.info("userObject======================:"+userObject);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。