知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們在追求其視覺表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 公眾號(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)頁授權(quán)兩次或多次重定響應(yīng)問題,我這里的scope=snsapi_base不會(huì)彈出授權(quán)頁面,但只能拿到用戶的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è)置通用的請求屬性
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--------幾萬個(gè)"+result);
result += line;
}
} catch (Exception e) {
System.out.println("發(fā)送GET請求出現(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)頁授權(quán)域名
在公眾號(hào)的【開發(fā)】-【接口權(quán)限】-【網(wǎng)頁授權(quán)獲取用戶基本信息】-【網(wǎng)頁授權(quán)域名】中配置域名
沒有正式公眾號(hào)的可以用測試號(hào)試用:http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
相關(guān)案例查看更多
相關(guān)閱讀
- 汽車拆解管理系統(tǒng)
- 云南小程序開發(fā)推薦
- 小程序開發(fā)公司
- 云南小程序開發(fā)報(bào)價(jià)
- 麗江小程序開發(fā)
- 迪慶小程序開發(fā)
- 網(wǎng)站建設(shè)公司地址
- 小程序定制開發(fā)
- web學(xué)習(xí)路線
- 云南網(wǎng)站建設(shè)哪家強(qiáng)
- 小程序開發(fā)費(fèi)用
- 做小程序被騙
- 小程序生成海報(bào)
- 云南軟件公司
- 百度快速排名
- 汽車拆解管理軟件
- 云南網(wǎng)站建設(shè)電話
- 商標(biāo)注冊
- 報(bào)廢車
- 開發(fā)制作小程序
- 小程序用戶登錄
- 網(wǎng)頁制作
- 云南網(wǎng)站設(shè)計(jì)
- 網(wǎng)站優(yōu)化哪家好
- 退款
- 貴州小程序開發(fā)
- 北京小程序制作
- 百度自然排名
- 網(wǎng)站建設(shè)優(yōu)化
- 關(guān)鍵詞快速排名