<%@ 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了。
版权声明:本文为博主原创文章,未经博主允许不得转载。
第三方平台代公众号发起网页授权OAuth2.0,获取openid
原文地址:http://blog.csdn.net/u013628152/article/details/46988703