微信公众号开发中使用Java如何实现获取用户的信息

  介绍

微信公众号开发中使用Java如何实现获取用户的信息?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1,首先需要到微信网站去设置一下,我是直接用的微信测试号。

接口配置信息必须要填写的,所以说必须能将自己的服务发布出去

微信公众号开发中使用Java如何实现获取用户的信息

微信公众号开发中使用Java如何实现获取用户的信息

微信公众号开发中使用Java如何实现获取用户的信息

微信公众号开发中使用Java如何实现获取用户的信息

,,,,,,,,,,,,,,,,,,,,,,,,,,,,到此微信配置完毕,接下来就是直接上代码了

2,获取用户信息的方式一共是两种,前提都是用户关注微信公众号,一种是静默获取(snsapi_base,这种方式只能获取openid),另一种是授权获取(snsapi_userinfo,可以获取用户的详细信息)。

,,,,,先说第一种

(1)首先需要先访问微信的链接

https://open.weixin.qq.com/connect/oauth3/authorize& # 63; appid=xxxxxxxxxxxxxxxx& redirect_uri=http://xxxxxx/open/openid& response_type=code&范围=snsapi_base

,,,,,,,,,,就这里的uri是直接回掉我们的服务地址,一定要记住,服务校验的判断,我是按照来判断的echostr(第二种方式也是这样)

包net.itraf.controller;
  进口java.io.IOException;
  进口java.io.InputStream;
  进口java.io.PrintWriter;
  进口java.net.HttpURLConnection;
  进口java.net.MalformedURLException;
  进口java.net.URL;
  进口javax.servlet.http.HttpServletResponse;
  进口org.springframework.stereotype.Controller;
  进口org.springframework.web.bind.annotation.RequestMapping;
  进口org.springframework.web.bind.annotation.ResponseBody;
  进口com.alibaba.fastjson.JSONObject;
  @ controller
  @RequestMapping (“/open")
  公开课OpenController {
  @RequestMapping (“/toOpenId")
  公共getOpenId @ResponseBody字符串(字符串的代码,字符串echostr HttpServletResponse res)抛出IOException {
  如果(echostr==null) {
  字符串url=癶ttps://api.weixin.qq.com/sns/oauth3/access_token& # 63; appid=wx24d47d2080f54c5b&秘密=95011 ac70909e8cca2786217dd80ee3f&代码=?代码+“,grant_type=authorization_code";
  System.out.println(代码);
  字符串openId=?“;
  尝试{
  URL getUrl=新网址(URL);
  HttpURLConnection http=(HttpURLConnection) getUrl.openConnection ();
  http.setRequestMethod (“GET");
  http.setRequestProperty (“Content-Type",“应用程序/x-www-form-urlencoded");
  http.setDoOutput(真正的);
  http.setDoInput(真正的);
  http.connect ();
  InputStream是=http.getInputStream ();
  int大?is.available ();
  byte [] b=新的字节(大小);
  is.read (b);
  字符串消息=新的字符串(b,“UTF-8");
  JSONObject json=JSONObject.parseObject(消息);
  openId=json.getString (“openid");
  }捕捉(MalformedURLException e) {
  e.printStackTrace ();
  }捕捉(IOException e) {
  e.printStackTrace ();
  }
  返回openId;
  其他}{
  PrintWriter=res.getWriter ();
  都会(echostr);
  返回null;
  }
  }//做服务器校验
  @RequestMapping (“/tovalid")
  公共空间有效(String echostr HttpServletResponse res)抛出IOException {
  PrintWriter=res.getWriter ();
  都会(echostr);
  }
  }

第二种

(1)

https://open.weixin.qq.com/connect/oauth3/authorize& # 63; appid=xxxxxxxx& redirect_uri=http:// 域名

/打开/openid& response_type=code&范围=snsapi_userinfo&状态=1,connect_redirect=1 # wechat_redirect

包net.itraf.controller;
  进口java.io.IOException;
  进口java.io.PrintWriter;
  进口javax.servlet.ServletException;
  进口javax.servlet.http.HttpServletRequest;
  进口javax.servlet.http.HttpServletResponse;
  进口net.sf.json.JSONObject;
  进口org.springframework.stereotype.Controller;
  进口org.springframework.web.bind.annotation.RequestMapping;
  @ controller
  @RequestMapping (“/weixin")
  公开课Oauth3Action {
  @RequestMapping (“/oauth")
  公共空间身份验证(HttpServletRequest请求,HttpServletResponse响应)
  抛出ServletException IOException {
  字符串echostr=request.getParameter (“echostr");
  如果(echostr==null) {
  字符串appId=皐x24d47d2080f54c5b";
  字符串appSecret=?5011 ac70909e8cca2786217dd80ee3f";//拼接
  字符串get_access_token_url=癶ttps://api.weixin.qq.com/sns/oauth3/access_token& # 63;“
  +“appid=?
  + appId
  +“,秘密=?
  + appSecret
  +“,代码=CODE& grant_type=authorization_code";
  字符串get_userinfo=癶ttps://api.weixin.qq.com/sns/userinfo& # 63; access_token=ACCESS_TOKEN& openid=OPENID& lang=zh_CN";
  request.setCharacterEncoding (“UTF-8");
  response.setCharacterEncoding (“UTF-8");
  字符串代码=request.getParameter (“code");
  System.out.println(“* * * * * * * * * * * * * * * * * *代码=?+代码);
  get_access_token_url=get_access_token_url.replace (“CODE"、代码);
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

微信公众号开发中使用Java如何实现获取用户的信息