微信公众平台-openid获取

来源:互联网 发布:windows loader2.2.1 编辑:程序博客网 时间:2024/05/16 04:14

第一步:理解逻辑。

获得微信的openid,需要先访问微信提供的一个网址:这个网址名为url1,下面有赋值。通过这个网址,微信用来识别appid信息,在这个网址中,有一个属性redirect_uri,是微识别完appid后,进行跳转的操作,可以是网页,也可以是servlet,我这里用的是servlet,微信跳转到这个servlet中,会传递一个code值,我们用这个code值,再访问微信提供的另一网址url2,下面有赋值,则可以获得json类型的返回数据,其中就有我们需要的openid。

String url1 = https://open.weixin.qq.com/connect/oauth2/authorizeappid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirectString 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即可!

第三步:代码:

servlet中的代码:

String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&stat e=STATE#wechat_redirect";    try {    String appid= "wx804e5da12c0a50c0";    String REDIRECT_URI = "http://ipcloud.lstn.cn:8081/PeanutWx/UserOpenIDServlet";    url  = url.replace("APPID", WxMenuUtils.urlEnodeUTF8(appid));     url  = url.replace("REDIRECT_URI",WxMenuUtils.urlEnodeUTF8(REDIRECT_URI));    response.sendRedirect(url);} catch (Exception e) {e.printStackTrace();}

其中getopendid()方法的代码:

public static String getopendid(String code) throws ParseException, IOException {    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;}

最后附上模拟浏览器提交请求的代码:

import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.conn.ClientConnectionManager;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.impl.client.DefaultHttpClient;public class HttpClientConnectionManager {/*** 获取SSL验证的HttpClient* @param httpClient* @return*/public static HttpClient getSSLInstance(HttpClient httpClient){ClientConnectionManager ccm = httpClient.getConnectionManager();SchemeRegistry sr = ccm.getSchemeRegistry();sr.register(new Scheme("https", MySSLSocketFactory.getInstance(), 443));httpClient =  new DefaultHttpClient(ccm, httpClient.getParams());return httpClient;}/*** 模拟浏览器post提交* * @param url* @return*/public static HttpPost getPostMethod(String url) {HttpPost pmethod = new HttpPost(url); // 设置响应头信息pmethod.addHeader("Connection", "keep-alive");pmethod.addHeader("Accept", "*/*");pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");pmethod.addHeader("Host", "mp.weixin.qq.com");pmethod.addHeader("X-Requested-With", "XMLHttpRequest");pmethod.addHeader("Cache-Control", "max-age=0");pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");return pmethod;}/*** 模拟浏览器GET提交* @param url* @return*/public static HttpGet getGetMethod(String url) {HttpGet pmethod = new HttpGet(url);// 设置响应头信息pmethod.addHeader("Connection", "keep-alive");pmethod.addHeader("Cache-Control", "max-age=0");pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");pmethod.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");return pmethod;}}

只要按照逻辑来,在加上附上的代码,可以获得openid。

原创粉丝点击