微信公众号获取openid

原文链接: 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,必须将前置条件配置成功,前置条件配置说明链接:

posted @ 2022-03-23 18:48  枫树湾河桥  阅读(7646)  评论(0编辑  收藏  举报
Live2D