首先准备一个已经认证的微信公众号
假如没有已经认证的,就可以使用 公众平台测试账号 进行开发
然后看:
点击 进入,查看此接口的开发说明文档。
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
大概有4步
1 第一步:用户同意授权,获取code
2 第二步:通过code换取网页授权access_token
3 第三步:刷新access_token(如果需要)
4 第四步:拉取用户信息(需scope为 snsapi_userinfo)
5 附:检验授权凭证(access_token)是否有效
下面进行代码的开发:
新建一个项目:WxAuth
下面编写一个工具类:
根据地址进行网络请求的
需要的jar包
/WxAuth/src/com/wx/auth/util/AuthUtil.java
package com.wx.auth.util;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
public class AuthUtil {
public static final String APPID = "wxa2021f7d0c2259f9";
public static final String APPSECRET = "be070e2d7ee644aaa845f81f2cd847b2";
public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException{
JSONObject jsonObject = null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity != null){
String result = EntityUtils.toString(entity,"UTF-8");
jsonObject = JSONObject.fromObject(result);
}
httpGet.releaseConnection();
return jsonObject;
}
}
下面组装一个接口地址:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
/WxAuth/src/com/wx/auth/util/LoginServlet.java
package com.wx.auth.servlet;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/wxLogin")
public class LoginServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//回调地址,需公网能访问
String backUrl = "http://huanglianggu.s1.natapp.cc/WxAuth/callBack";
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AuthUtil.APPID
+ "&redirect_uri="+URLEncoder.encode(backUrl)
+ "&response_type=code"
+ "&scope=snsapi_userinfo"
+ "&state=STATE#wechat_redirect";
resp.sendRedirect(url);
}
}
第二步:通过code换取网页授权access_token
获取code后,请求以下链接获取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
/WxAuth/src/com/wx/auth/servlet/CallBackServlet.java
package com.wx.auth.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import com.wx.auth.util.AuthUtil;
@WebServlet("/callBack")
public class CallBackServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String code = req.getParameter("code");
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+AuthUtil.APPID
+ "&secret="+AuthUtil.APPSECRET
+ "&code="+code
+ "&grant_type=authorization_code";
// 进行网络请求
JSONObject jsonObject = AuthUtil.doGetJson(url);
String openid = jsonObject.getString("openid");
String token = jsonObject.getString("access_token");
// 第四步:拉取用户信息(需scope为 snsapi_userinfo)
//String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+token
+"&openid="+openid
+"&lang=zh_CN";
// 进行网络请求
JSONObject userInfo = AuthUtil.doGetJson(infoUrl);
System.out.println("userInfo = "+userInfo);
super.doGet(req, resp);
}
}
再创建一个页面
/WxAuth/WebContent/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width-device-width,initial-scale-1.8">
<title>Insert title here</title>
</head>
<body style="font-size:45px;text-align:center;">
<a href="/WxAuth/wxLogin">微信公众授权登录</a>
</body>
</html>
启动项目
浏览器访问:
http://huanglianggu.s1.natapp.cc/WxAuth/index.jsp
如果浏览器点击 则会提示
需要手机登录
手机上点击登录后,提示:
需要到 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
修改:
点击 修改
手机点击登录,控制台是输出想要的结果了。
但是,回调页面报错:
直接跳转到 resp.sendRedirect("http://www.baidu.com");
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String code = req.getParameter("code");
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+AuthUtil.APPID
+ "&secret="+AuthUtil.APPSECRET
+ "&code="+code
+ "&grant_type=authorization_code";
// 进行网络请求
JSONObject jsonObject = AuthUtil.doGetJson(url);
String openid = jsonObject.getString("openid");
String token = jsonObject.getString("access_token");
// 第四步:拉取用户信息(需scope为 snsapi_userinfo)
//String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+token
+"&openid="+openid
+"&lang=zh_CN";
// 进行网络请求
JSONObject userInfo = AuthUtil.doGetJson(infoUrl);
System.out.println("userInfo = "+userInfo);
resp.sendRedirect("http://www.baidu.com");
//super.doGet(req, resp);
}