微信网页授权获取用户基本信息
2023-09-27 14:21:59 时间
1、准备工作
1)申请测试账号,并接入自已的servlet
@WebServlet("/wx_token") public class wx_token extends HttpServlet { private static final long serialVersionUID = 1L; public String token = "xxx"; public wx_token() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); System.out.println("signature:" + signature); System.out.println("timestamp:" + timestamp); System.out.println("nonce:" + nonce); System.out.println("echostr:" + echostr); PrintWriter pw = response.getWriter(); pw.append(echostr); pw.flush(); } }
servlet中的token跟这个配制一制即可,如果是xxx,那大家都是xxx.
下载natapp
将本地地址转到外网地址
如果是tomcat,将域名直接访问
通过以上的访问,可以直接通过localhost访问到ckySvr站
打开natapp.exe,可以看到http://开头的域名,转到了本地80端口,
打开微信测试账号管理页,找到
这样微信中就配好了。
下面是实际的Servlet,这个Servlet主要是获取code
@WebServlet("/OpenIdServlet") public class OpenIdServlet extends HttpServlet { private static final long serialVersionUID = 1L; private String openidURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; private String appId = "您的appId"; private String redirectURL = "http://10ec6069.ngrok.natapp.cn/OpenIdResultServlet"; public OpenIdServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = String.format(openidURL, appId, redirectURL); response.sendRedirect(url); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
@WebServlet("/OpenIdResultServlet") public class OpenIdResultServlet extends HttpServlet { private static final long serialVersionUID = 1L; private String appId = "您的appId"; private String appSecret = "您的appSecret"; /** * @see HttpServlet#HttpServlet() */ public OpenIdResultServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); String url = "https://api.weixin.qq.com/sns/oauth2/access_token"; String params = "appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code"; // request.setAttribute("code", url + "?" + params); String s = HttpRequest.sendGet(url, params); request.setAttribute("code", s); try { JSONObject jsonObject = new JSONObject(s); String openid = jsonObject.getString("openid"); request.setAttribute("code", "openid:" + openid); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } request.getRequestDispatcher("result.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class HttpRequest { /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */ public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } }
这样openid就输出在了页面上了。即加密的微信号
注意:测试账号,是要关注了才可以获取的到,认证的账号应该不要,具体的要测试。
具体请参考微信官网说明:
下面就是实际测试了,这些离开了微信客户端就没有意义了。
所以打开扫一扫,扫一下我们初始的Servlet地址对应的URL
这样你就可以在微信客户端里看到了openid,即加密的微信号
相关文章
- 使用docker mediawiki,搭建网页wiki
- 查看网页源码的时候找不到数据绑定
- PHP 实现微信公众号网页授权登录
- 微信公众号 - 实现 H5 网页在微信内置浏览器中下载文件,可预览和下载 office 文件(doc / xls / ppt / pdf 等)适用于任何前端技术栈网站,兼容安卓和苹果系统!
- 微信公众号 - 第三方网页(H5)授权登录(OAuth2.0)流程及逻辑详细教程
- 微信公众号 - 超详细 H5 网页接入微信支付功能,开发网站微信支付最详细的教程示例代码(微信公众号前端支付 JS-SDK / JSAPI 微信H5授权登录支付,唤起微信支付)最新超级详细教程
- JavaScript - H5 网页拨打电话功能、发送短信、发送邮件(支持 HTML 与 JS 形式)点击自动调起手机拨打并填充手机号
- 如何在终端下截取一个完整长度的网页截图
- 通过网页修改AD密码 AD用户密码
- css代码书写的时候按照什么格式书写(网页布局的思路)
- 手把手教你快应用接入微信H5网页支付
- JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
- 《HTML、CSS、JavaScript 网页制作从入门到精通》——1.4 练习题
- 《网页设计与前端开发 Dreamweaver+Flash+Photoshop+HTML+CSS+JavaScript 从入门到精通》——1.2 网页的基本构成元素
- 《移动网页设计与开发 HTML5+CSS3+JavaScript》—— 1.2 你是谁,你需要知道些什么
- SwiftUI 爬虫大全之如何使用SwiftUI抓取网页并显示解析内容,支持搜索(教程含源码)
- 微信点击超链接第一次不会有“微信中网页底部导航栏”,进入之后再点击链接就会有了,自己会新建窗口
- 使用C语言在windows下一口气打开一批网页
- js网页滚动条滚动事件实例分析
- js实现类似微信网页版在可编辑的div中粘贴内容时过滤剪贴板的内容,光标始终在粘贴内容后面,以及将光标定位到最后的方法
- 解决微信分享报错config:fail,invalid signature的问题、微信网页开发invalid signature/permission denied/offline verifying等错误处理
- Python实例---模拟微信网页登录(day5)
- Python学习---模拟微信网页登录180410
- 用Canvas为网页加入动态背景
- 安卓中一句代码,解决拨号,定位,网页,卸载,安装,播放,邮件,短信
- 点击网页分享按钮,触发微信分享功能