欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

獲取微信公眾號(hào)用戶的openid - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 公眾號(hào)相關(guān) >

獲取微信公眾號(hào)用戶的openid

發(fā)表時(shí)間:2020-11-6

發(fā)布人:葵宇科技

瀏覽次數(shù):92

前言:

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

1、用戶同意授權(quán),獲取code

public void getCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//appid是公眾號(hào)的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)進(jìn)行編碼,在連接中加入 connect_redirect=1 是防止網(wǎng)頁(yè)授權(quán)兩次或多次重定響應(yīng)問題,我這里的scope=snsapi_base不會(huì)彈出授權(quán)頁(yè)面,但只能拿到用戶的openid

2、在回調(diào)地址中通過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();
            // 設(shè)置通用的請(qǐng)求屬性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立實(shí)際的連接
            connection.connect();
            // 獲取所有響應(yīng)頭字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍歷所有的響應(yīng)頭字段
            for (String key : map.keySet()) {
            }
            // 定義 BufferedReader輸入流來讀取URL的響應(yīng)
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
            	//System.out.println("jieguo--------幾萬(wàn)個(gè)"+result);
                result += line;
            }
        } catch (Exception e) {
            System.out.println("發(fā)送GET請(qǐng)求出現(xiàn)異常!" + e);
            e.printStackTrace();
        }
        // 使用finally塊來關(guān)閉輸入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
	

3、公眾號(hào)配置網(wǎng)頁(yè)授權(quán)域名

在公眾號(hào)的【開發(fā)】-【接口權(quán)限】-【網(wǎng)頁(yè)授權(quán)獲取用戶基本信息】-【網(wǎng)頁(yè)授權(quán)域名】中配置域名

沒有正式公眾號(hào)的可以用測(cè)試號(hào)試用:http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

相關(guān)案例查看更多