原文鏈接: https://blog.csdn.net/qq_24800377/article/details/53437040
今天做微信公眾號獲取用戶的openid,圓滿成功,特此來一發。
第一步:理解邏輯。
1:獲取openid的邏輯
獲得微信的openid,需要先訪問微信提供的一個網址:這個網址名為url1,下面有賦值。
通過這個網址,微信用來識別appid信息,在這個網址中,有一個屬性redirect_uri,是微識別完appid后,進行跳轉的操作,可以是網頁,也可以是servlet,我這里用的是servlet。
微信跳轉到這個servlet中,會傳遞一個code值,我們用這個code值,再訪問微信提供的另一網址url2,下面有賦值。
則可以獲得json類型的返回數據,其中就有我們需要的openid
url1:
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
"appid=APPID" +
"&redirect_uri=REDIRECT_URI" +
"&response_type=code" +
"&scope=snsapi_base" +
"&state=STATE" +
"#wechat_redirect";
url2:
String url2 = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=AppId" +
"&secret=AppSecret" +
"&code=CODE" +
"&grant_type=authorization_code";
第二步:注意事項
知道邏輯之后,我們需要具體操作,在實際操作中,我們還需要注意幾點,首先,是理解我們第一個訪問的網址url1,它有6個參數。
appid,填寫自己的appid.
redirect_uri,填寫微信識別成功之后,跳轉的url(需要encode編碼)。
response_type,就填code,不用修改。
scope,可填(snsapi_base和snsapi_userinfo兩個值,其中前者為只獲得openid,不需要用戶授權,后者為獲得用戶信息,需要用戶授權)
state,自定義參數,可隨意填也可不填。
#wechat_redirect,指定在微信內跳轉,平時可以不填,在302重定向時,必須填!
這里值得注意的有兩點,第一點,redirect_uri需要encode編碼,否則頁面會顯示“redirect_ur參數錯誤!”!
第二點,redirect_uri網址的域名必須是,你在微信公眾平台賬號中填寫授權回調頁的域名,具體需要登錄微信公眾平台后台,在用戶信息那里點擊修改,填上自己的域名即可,注意:授權回調頁中的域名沒有http://!
理解第二個網址,它有4個參數。
appid,登錄公眾號 就有。
secret,登錄公眾號就有。
code,訪問url1,在servlet中,獲得code。
grant_type,不用改,填它authorization_code即可!
第三步:代碼:
用戶點擊按鈕后,進入到后台,后台訪問微信網址url1;
@RequestMapping(value = "${adminPath}/xxx")
public void getOpenId(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
String url = WxUtils.getOpenIdUrl("xiaoming");
System.out.println("微信網址:"+url);
response.sendRedirect(url);
}
其中
getOpenIdUrl()方法的代碼:
public static String getOpenIdUrl(String username) throws ClientProtocolException, IOException {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
String appid = "你的appid";
String REDIRECT_URI = "http://www.xxx.cn/xxx/xxx/xxx/xxx";//你的回調頁
url = url.replace("APPID", urlEnodeUTF8(appid));
url = url.replace("STATE", username);
url = url.replace("REDIRECT_URI", urlEnodeUTF8(REDIRECT_URI));
return url;
}
訪問之后,如果成功,微信會自動訪問url2,也就是你的回調頁:
@RequestMapping(value = "xxx/xxx/xxx")
public void getOpenId2(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
String code = request.getParameter("code");//微信活返回code值,用code獲取openid
// String url = WxUtils.getOpenIdUrl2(code);
String openId = WxUtils.getopendid(code);
System.out.println("openId:"+openId);
}
其中getopendid()方法代碼:
public static String getopendid(String code) throws ParseException, IOException {
String appid = "wxxxxxxxx";
String secret = "f08c8xxxxxxxxxxxx";
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=AppId&secret=AppSecret&code=CODE&grant_type=authorization_code";
url = url.replace("AppId", appid)
.replace("AppSecret", secret)
.replace("CODE", code);
HttpGet get = HttpClientConnectionManager.getGetMethod(url);
HttpResponse response = httpclient.execute(get);
String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject jsonTexts = (JSONObject) JSON.parse(jsonStr);
String openid = "";
if (jsonTexts.get("openid")!=null) {
openid = jsonTexts.get("openid").toString();
}
return openid;
}
到此搞定!可以獲得openid。
工具類的下載地址:https://download.csdn.net/download/qq_24800377/10434042
注意事項:獲取openid,必須將前置條件配置成功,前置條件配置說明鏈接: