如何通过微信公众号快速获取用户唯一标识openid?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1003个文字,预计阅读时间需要5分钟。
原文链接:https://blog.csdn.net/qq_24800377/article/details/53437040
今日任务:获取微信公众号新用户openid,圆满成功,特此一发展。
第一步:理解逻辑。
1:获取openid的逻辑 获取微信的openid,首先需要获得微信的授权,然后通过授权获取用户的openid。具体步骤如下: 1. 访问公众号的URL,获取code; 2. 使用code换取网页授权access_token; 3. 使用access_token换取用户的openid。
原文链接: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 = "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 = "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网址的域名必须是,你在微信公众平台账号中填写授权回调页的域名,具体需要登录微信公众平台后台,在用户信息那里点击修改,填上自己的域名即可,注意:授权回调页中的域名没有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 = "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 = "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 = download.csdn.net/download/qq_24800377/10434042
注意事项:获取openid,必须将前置条件配置成功,前置条件配置说明链接:
本文共计1003个文字,预计阅读时间需要5分钟。
原文链接:https://blog.csdn.net/qq_24800377/article/details/53437040
今日任务:获取微信公众号新用户openid,圆满成功,特此一发展。
第一步:理解逻辑。
1:获取openid的逻辑 获取微信的openid,首先需要获得微信的授权,然后通过授权获取用户的openid。具体步骤如下: 1. 访问公众号的URL,获取code; 2. 使用code换取网页授权access_token; 3. 使用access_token换取用户的openid。
原文链接: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 = "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 = "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网址的域名必须是,你在微信公众平台账号中填写授权回调页的域名,具体需要登录微信公众平台后台,在用户信息那里点击修改,填上自己的域名即可,注意:授权回调页中的域名没有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 = "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 = "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 = download.csdn.net/download/qq_24800377/10434042
注意事项:获取openid,必须将前置条件配置成功,前置条件配置说明链接:

