获取微信公众号用户的openid - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 公众号相关 >

获取微信公众号用户的openid

发表时间:2020-11-6

发布人:葵宇科技

浏览次数:66

前言:

官方参考连接:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

1、用户同意授权,获取code

public void getCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//appid是公众号的appid
		String redirect_uri=Param.local_url+"wx/openid";
		String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri="
				+ URLEncoder.encode(redirect_uri, "GBK")
				+ "&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect";
		response.sendRedirect(url);
	}

需要注意的是链接需要用 URLEncoder.encode(url)进行编码,在连接中加入 connect_redirect=1 是防止网页授权两次或多次重定响应问题,我这里的scope=snsapi_base不会弹出授权页面,但只能拿到用户的openid

2、在回调地址中通过code获取用户的openid

public void openid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml; charset=UTF-8");
		String oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + AppSecret
				+ "&code=" + request.getParameter("code") + "&grant_type=authorization_code";
		JSONObject jsonObject = JSONObject.fromObject(HttpRequestUtil.sendGet(oauth2Url));
		String pubopenid = jsonObject.getString("openid");
		System.out.println("获取的openid"+pubopenid);
		
}

其中的sendGet()方法如下:

public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
            	//System.out.println("jieguo--------几万个"+result);
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
	

3、公众号配置网页授权域名

在公众号的【开发】-【接口权限】-【网页授权获取用户基本信息】-【网页授权域名】中配置域名

没有正式公众号的可以用测试号试用:http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

相关案例查看更多