网页授权获取用户基本信息接口,经常获取不到openid
/*点击菜单跳转的方法*/
String code = request.getParameter("code");
WachatAjaxDomain openIdDomain = CommonController.getInstance().getOpenId(code);
if (openIdDomain==null || openIdDomain.getMap()==null || openIdDomain.getMap().get("openid")==null) {//获取openid失败,重新请求
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=...&redirect_uri=...&flag=1&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
//记录日志
SystemLog systemLog = new SystemLog(1, "获取openid超时,重新跳转请求。", null, null);
systemLogService.addSystemLog(systemLog);
return new ModelAndView(url);
}
String openid = (String) openIdDomain.getMap().get("openid");
String flag = request.getParameter("flag");
if (flag != null) {
//记录日志
SystemLog systemLog = new SystemLog(20, "获取openid超时,重新跳转请求。", null, flag);
systemLogService.addSystemLog(systemLog);
}
/**获取openid的方法*/
WachatAjaxDomain ajaxDomain = new WachatAjaxDomain();
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WachatCommon.WEIXIN_APPID+"&secret="+WachatCommon.WEIXIN_SECRET+"&grant_type=authorization_code&code="+code;
URL U = null;
try {
U = new URL(url);
URLConnection connection = U.openConnection();
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String resultStr = "";
while ((line = in.readLine()) != null) {
resultStr += line;
}
in.close();
JSONObject jsonObject = JSONObject.fromObject(resultStr);
Object errcode = jsonObject.get("errcode");
if(errcode==null){
ajaxDomain.setStatusCode("200");
Map<String, Object> mapRe = new HashMap<String, Object>();
mapRe.put("openid", jsonObject.get("openid"));
mapRe.put("expires_in", jsonObject.get("expires_in"));
mapRe.put("scope", jsonObject.get("scope"));
mapRe.put("access_token", jsonObject.get("access_token"));
ajaxDomain.setMap(mapRe);
}else{
ajaxDomain.setStatusCode("300");
ajaxDomain.setMessage(jsonObject.getString("errmsg"));
}
} catch (Exception e) {
e.printStackTrace();
ajaxDomain.setMessage("获取access_token异常!");
}
return ajaxDomain;
现在大概有两三千个用户,一天会产生几百条用户获取不到openid的数据库插入记录。求大神分析一下原因,谢谢了。
String code = request.getParameter("code");
WachatAjaxDomain openIdDomain = CommonController.getInstance().getOpenId(code);
if (openIdDomain==null || openIdDomain.getMap()==null || openIdDomain.getMap().get("openid")==null) {//获取openid失败,重新请求
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=...&redirect_uri=...&flag=1&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
//记录日志
SystemLog systemLog = new SystemLog(1, "获取openid超时,重新跳转请求。", null, null);
systemLogService.addSystemLog(systemLog);
return new ModelAndView(url);
}
String openid = (String) openIdDomain.getMap().get("openid");
String flag = request.getParameter("flag");
if (flag != null) {
//记录日志
SystemLog systemLog = new SystemLog(20, "获取openid超时,重新跳转请求。", null, flag);
systemLogService.addSystemLog(systemLog);
}
/**获取openid的方法*/
WachatAjaxDomain ajaxDomain = new WachatAjaxDomain();
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WachatCommon.WEIXIN_APPID+"&secret="+WachatCommon.WEIXIN_SECRET+"&grant_type=authorization_code&code="+code;
URL U = null;
try {
U = new URL(url);
URLConnection connection = U.openConnection();
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String resultStr = "";
while ((line = in.readLine()) != null) {
resultStr += line;
}
in.close();
JSONObject jsonObject = JSONObject.fromObject(resultStr);
Object errcode = jsonObject.get("errcode");
if(errcode==null){
ajaxDomain.setStatusCode("200");
Map<String, Object> mapRe = new HashMap<String, Object>();
mapRe.put("openid", jsonObject.get("openid"));
mapRe.put("expires_in", jsonObject.get("expires_in"));
mapRe.put("scope", jsonObject.get("scope"));
mapRe.put("access_token", jsonObject.get("access_token"));
ajaxDomain.setMap(mapRe);
}else{
ajaxDomain.setStatusCode("300");
ajaxDomain.setMessage(jsonObject.getString("errmsg"));
}
} catch (Exception e) {
e.printStackTrace();
ajaxDomain.setMessage("获取access_token异常!");
}
return ajaxDomain;
现在大概有两三千个用户,一天会产生几百条用户获取不到openid的数据库插入记录。求大神分析一下原因,谢谢了。
...全文
相关推荐
微信网页授权获取用户openid以及用户信息demo下载
本教程将详细讲解如何使用Java实现微信网页授权获取用户的openid及用户信息。首先,我们要理解微信网页授权的基本流程。
微信公众号网页授权获取openid等用户基本信息
该项目实现了微信公众号网页授权流程,通过OAuth2.0协议获取用户OpenID及基本信息。核心逻辑包括重定向至微信授权页、使用code换取access_token和OpenID,并可进一步拉取用户资
uniapp微信h5授权获取用户openId的方法和步骤,用于用户登录和注册
该项目实现了UniApp框架下微信H5页面授权获取用户OpenID的功能,通过微信OAuth2.0授权流程获取code,再请求后端换取用户标识与token,完成登录注册。包含页面权限控制逻辑,未登录自
ASP.NET网页微信登录授权,获取用户基本信息
本文介绍了一个ASP.NET页面类,用于处理微信授权流程。页面加载时会根据查询字符串参数决定是否弹出微信授权页面,并重定向用户获取授权码。授权成功后,用户将被重定向回指定页面,系统通过code获取ac
微信 网页授权获取用户基本信息(asp.net C#)
该项目基于ASP.NET C#实现微信网页授权登录功能,利用OAuth2.0协议获取用户的access_token和openid,并进一步拉取用户基本信息(如昵称、性别、头像等)。通过code换取令牌


