微信用户已经关注公众号,获取的用户信息却是未关注状态(subscribe=0)

qq_42466507 2019-06-06 03:17:19
微信用户已经关注公众号,获取的用户信息却是未关注状态(subscribe=0)
...全文
695 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ziyuewxx 2019-06-17
  • 打赏
  • 举报
回复
 @RequestMapping("/weChatGetCode")
public String weChatGetCode() throws UnsupportedEncodingException {
String redirect_uri=URLEncoder.encode("http://*********/weChatUserFollow", "UTF-8");
StringBuffer url=new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?redirect_uri="+redirect_uri+
"&appid="+resource.getString("wx.appid")+"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
return "redirect:"+url;
}

@RequestMapping("/weChatUserFollow")
public @ResponseBody String weChatUserFollow(HttpServletRequest request){
HttpSession session = request.getSession();
String code = request.getParameter("code");//获取code
Map params = new HashMap();
params.put("secret", resource.getString("wx.secret"));
params.put("appid", resource.getString("wx.appid"));
params.put("grant_type", "authorization_code");
params.put("code", code);
String result = HttpGetUtil.httpRequestToString(
"https://api.weixin.qq.com/sns/oauth2/access_token", params);
JSONObject jsonObject = JSONObject.parseObject(result);

String openid = jsonObject.get("openid").toString();
session.setAttribute("openid",openid);
String access_token=jsonObject.get("access_token").toString();
Map params2 = new HashMap();
params2.put("access_token",access_token);
params2.put("openid",openid);
params2.put("lang","zh_CN");
String rs2 = HttpGetUtil.httpRequestToString("https://api.weixin.qq.com/cgi-bin/user/info", params2);
JSONObject jsonObject2 = JSONObject.parseObject(rs2);

if(jsonObject2.get("subscribe").toString().equals("1")){
return JSON.toJSONString("true");
}else{
return JSON.toJSONString("false");
}
}
Gemini_Kanon 2019-06-10
  • 打赏
  • 举报
回复
获取到用户的openid了没
qq_42466507 2019-06-10
  • 打赏
  • 举报
回复
获取到了openid

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧