package com.it.service;
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 com.it.util.AutUtil;
import net.sf.json.JSONObject;
/**
* 回调地址
* @author
*
*/
@WebServlet("/callBack")
public class callBackService extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//微信服务器访问callBack会带回code
String code = req.getParameter("code");
System.out.println(code);
//通过code得到access_token票据
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+AutUtil.APPID
+ "&secret="+AutUtil.APPSECRET
+ "&code="+code
+ "&grant_type=authorization_code";
JSONObject jsonObject = AutUtil.doget(url);
System.out.println(jsonObject.getString("access_token"));
System.out.println(jsonObject);
String openid = jsonObject.getString("openid");
System.out.println(openid);
String access_token = jsonObject.getString("access_token");
//拉取用户信息接口
String infourl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token
+ "&openid="+openid
+ "&lang=zh_CN";
JSONObject userinfo = AutUtil.doget(infourl);
System.out.println(userinfo);
}
}