1 Star 0 Fork 0

NUT/Study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
WeixinOauth 2.10 KB
一键复制 编辑 原始数据 按行查看 历史
NUT 提交于 2024-07-23 15:52 +08:00 . 微信授权登录获取微信上用户信息
@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);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tjcumath/study.git
git@gitee.com:tjcumath/study.git
tjcumath
study
Study
master

搜索帮助