官方文档地址
开放平台配置
单元测试test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String url = java.net.URLEncoder.encode("http://域名/oauth.jsp","UTF-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试</title>
</head>
<body>
<a href="https://open.weixin.qq.com/connect/oauth2/authorize?
appid=wxee1894c6b5a44b8e&redirect_uri=<%=url%>&
response_type=code&scope=snsapi_base&
state=2&
component_appid=wx630d7795f554888e
#wechat_redirect"><font size="5px">open平台base</font></a>
</body>
</html>
在微信浏览器中点击这个链接,微信就会调用回调redirect_uri,返回code,state,appid
在回调页面oauth.jsp中接受参数
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"";
String code = request.getParameter("code");
String state = request.getParameter("state");
String appid = request.getParameter("appid");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>获取openId</title>
</head>
<body>
code:<%=code%></br>
state:<%=state%></br>
appid:<%=appid%></br>
</body>
</html>
这样就可以获取到openid了。
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 12:17:46