• 微信公众平台--网页授权获取用户基本信息(snsapi_base方式)


    关于snsapi_base网页授权的说明

    以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)

    微信打开链接(https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx48414ee14f7d7158
    &redirect_uri=http://test.cn/testWx//servlet/Oauth2Servlet&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect)
    即可看到效果,效果截图如下:
    只打印了 openid。

    具体代码如下:(代码参考博客:http://www.cnblogs.com/zyw-205520/p/3581088.html)
     
    Oauth2Servlet.java
    package com.payroll.wx.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import net.sf.json.JSONObject;
    
    import com.payroll.wx.util.CommendDef;
    import com.payroll.wx.util.HttpsGetUtil;
    
    public class CopyOfOauth2Servlet extends HttpServlet {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        /**
         * The doGet method of the servlet. <br>
         * 
         * This method is called when a form has its tag value method equals to get.
         * 
         * @param request
         *            the request send by the client to the server
         * @param response
         *            the response send by the server to the client
         * @throws ServletException
         *             if an error occurred
         * @throws IOException
         *             if an error occurred
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //静默授权
            String get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
                    + "appid="
                    + CommendDef.AppId
                    + "&secret="
                    + CommendDef.AppSecret
                    + "&code=CODE&grant_type=authorization_code";
    
            // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            String code = request.getParameter("code");
    
            System.out.println("******************code=" + code);
    
            get_access_token_url = get_access_token_url.replace("CODE", code);
    
            String json = HttpsGetUtil.doHttpsGetJson(get_access_token_url);
    
            JSONObject jsonObject = JSONObject.fromObject(json);
            String openid = jsonObject.getString("openid");
    
            response.setContentType("text/html; charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
            out.println("<HTML>");
            out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println("  <BODY>");
            out.print("    This is ");
            out.print(this.getClass());
            out.println(", using the POST method 
    ");
            out.println("openid:" + openid + "
    
    ");
            out.println(">");
            out.println("  </BODY>");
            out.println("</HTML>");
            out.flush();
            out.close();
        }
    
    }
    
    
    HttpsGetUtil.java
    package com.payroll.wx.util;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class HttpsGetUtil
    {
    
        public static String doHttpsGetJson(String Url)
        {
            String message = "";
            try
            {
                System.out.println("doHttpsGetJson");//TODO:dd
                URL urlGet = new URL(Url);
                HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); 
                http.setRequestMethod("GET");      //必须是get方式请求    24           
                http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
                http.setDoOutput(true);  
                http.setDoInput(true);
                System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒28     
                System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒29 30       
                http.connect();
                InputStream is =http.getInputStream();
                int size =is.available();
                byte[] jsonBytes =new byte[size];
                is.read(jsonBytes);
                message=new String(jsonBytes,"UTF-8");
            } 
            catch (MalformedURLException e)
            {
                  e.printStackTrace();
              }
            catch (IOException e)
              {
                  e.printStackTrace();
              } 
            return message;
        }
    }
    
    
    
     
    
    
  • 相关阅读:
    【UOJ#77】A+B Problem
    【AGC048B】Bracket Score
    ubuntu 下python opengl编程(2)
    网站建设的营销途径
    python脚本初探---新手写的QQ邮箱发送脚本
    Cstyle的C语言笔记 ---UEFI当中的面向对象模式
    date得到当前日期
    简单几步让SecureCRT更舒服【图文并茂】
    苹果的airplayer推荐
    【Cocos2d-X开发学习笔记】第22期:事件处理机制之触屏事件
  • 原文地址:https://www.cnblogs.com/haha12/p/4303789.html
Copyright © 2020-2023  润新知