获取微信用户的openId

来源:互联网 发布:淘宝店铺网址怎么复制 编辑:程序博客网 时间:2024/06/06 17:25

开发框架:struts2(零配置)

官方文档下载地址

https://mp.weixin.qq.com/paymch/readtemplate?t=mp/business/course3_tmpl&lang=zh_CN

PS:下列获取openid的代码可以在柳峰的《微信公众平台应用开发方法、技巧与案例》的第六章找到。但是书中关于授权域名以及redirect_uri的关联写的不是很详细,在此主要详细介绍了出现问题排错的方向。代码觉得有疑惑的,可以看柳大神的书,或者csdn搜索柳峰找相关博客查看。

首先,我们需要进入我们的服务号,点击左侧栏开发者中心--->修改网页授权获取用户基本信息的值,假设我们对外的ip183.33.212.175tomcat的端口号为8016,这个修改为183.33.212.175:8016


创建WeiXinOauth2Token类。改类具有以下属性:(自行添加getset方法)

private String accessToken;

private int expiresIn;

private String refeshToken;

private String openId;

private String scope;

调用微信的授权接口

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect


其中要说明的是,redirect_uri一定是在我们设定网页授权获取用户基本信息的域名下的action,不然微信页面会提示redirect_uri错误(所以,出现该错误,各位应该知道如何排错了)

官方文档给了下面一段话解释:

比如需要网页授权的域名为:www.qq.com,配置以后此域名下面的页面http://www.qq.com/music.html、http://www.qq.com/login.html都可以进行 OAuth2.0 鉴权。但http://pay.qq.com、http://music.qq.com、http://qq.com 无法进行 OAuth2.0 鉴权。

上面一段话是什么意思呢?

假设我的授权域名为183.33.212.175:8016,那么。我的redirect_uri需要为http://183.33.212.175:8016/wxweb/config/oauth!execute.action特别注意的是前面的http://必须要有,

不然就提示找不到页面183.33.212.175:8016/wxweb/config /oauth!execute.action?code=XXXXXXXXXX。(这一点被坑了一天)

4 然后将redirect_uri进行encode,具体代码如下
public static String urlEncodeUTF8(String source){
                String result = source;
                try {
                        result = Java.NET.URLEncoder.encode(source,"utf-8");
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                }
                return result;
}
requestUrl= https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect中的REDIRECT_URI是我们的redirect_uri 进行encode后的值,APPID为你服务号的appid.
将上面的requestUrl设置为图文链接或者view按钮链接发送给用户。

5 获取用户openid 

我的redirect_uri对应的action方法为

public String execute() throws ServletException, IOException{
                // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
                HttpServletRequest request = ServletActionContext.getRequest();
                HttpServletResponse response = ServletActionContext.getResponse();
                request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8");
                String code = request.getParameter("code");
                String openId ="";
                if (!"authdeny".equals(code)) {
                        WeiXinOauth2Token weiXinOauth2Token = AdvancedUtil
                                        .getOauth2AccessToken("wx0953bae287adfeee",
                                                        "8e81dbc44a84a3c290c0cc3759f85421", code);
                        openId = weiXinOauth2Token.getOpenId();
                }
                request.getSession().setAttribute("openId", openId);
                return "index";
}

AdvancedUtil的方法如下:
public static WeiXinOauth2Token getOauth2AccessToken(String appId, String appSecret, String code) {
                WeiXinOauth2Token wat = new WeiXinOauth2Token();
                String requestUrl = oauth2Url.replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);
                JSONObject jsonObject = CommonUtil
                                .httpsRequest(requestUrl, "GET", null);
                if (null != jsonObject) {
                        try {
                                wat = new WeiXinOauth2Token();
                                wat.setAccessToken(jsonObject.getString("access_token"));
                                wat.setExpiresIn(jsonObject.getInt("expires_in"));
                                wat.setRefeshToken(jsonObject.getString("refresh_token"));
                                wat.setOpenId(jsonObject.getString("openid"));
                                wat.setScope(jsonObject.getString("scope"));
                        } catch (Exception e) {
                                wat = null;
                                String errorCode = jsonObject.getString("errcode");
                                String errorMsg = jsonObject.getString("errmsg");
                                log.error("获取网页授权凭证失败 errcode{},errMsg", errorCode, errorMsg);
                        }

                }
                return wat;
}
CommmonUtil相关方法如下
/**
         * 发送https请求
         * 
         * @param requestUrl 请求地址
         * @param requestMethod 请求方式(GET、POST)
         * @param outputStr 提交的数据
         * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
         */
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
                JSONObject jsonObject = null;
                try {
                        // 创建SSLContext对象,并使用我们指定的信任管理器初始化
                        TrustManager[] tm = { new MyX509TrustManager() };
                        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
                        sslContext.init(null, tm, new java.security.SecureRandom());
                        // 从上述SSLContext对象中得到SSLSocketFactory对象
                        SSLSocketFactory ssf = sslContext.getSocketFactory();
                        URL url = new URL(requestUrl);
                        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                        conn.setSSLSocketFactory(ssf);
                        conn.setDoOutput(true);
                        conn.setDoInput(true);
                        conn.setUseCaches(false);
                        // 设置请求方式(GET/POST)
                        conn.setRequestMethod(requestMethod);
                        //conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 
                        // 当outputStr不为null时向输出流写数据
                        if (null != outputStr) {
                                OutputStream outputStream = conn.getOutputStream();
                                // 注意编码格式
                                outputStream.write(outputStr.getBytes("UTF-8"));
                                outputStream.close();
                        }
                        // 从输入流读取返回内容
                        InputStream inputStream = conn.getInputStream();
                        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                        String str = null;
                        StringBuffer buffer = new StringBuffer();
                        while ((str = bufferedReader.readLine()) != null) {
                                buffer.append(str);
                        }
                        // 释放资源
                        bufferedReader.close();
                        inputStreamReader.close();
                        inputStream.close();
                        inputStream = null;
                        conn.disconnect();
                        jsonObject = JSONObject.fromObject(buffer.toString());
                } catch (ConnectException ce) {
                        log.error("连接超时:{}", ce);
                } catch (Exception e) {
                        log.error("https请求异常:{}", e);
                }
                return jsonObject;
}
测试OK后,会得到用户的openid并正确跳转到oauth_index.jsp页面。

假设用我们sae的应用,授权域名写为searchinfo.sinaapp.com,其中searchinfo是你的应用名称。那么没有encode前的redirect_uri为:http://searchinfo.sinaapp.com/config/oauth!execute.action。
需要注意的是,你部署的代码中config/oauth!execute.action方法所在版本必须为你应用的默认版本。检测是否可行,直接访问
searchinfo.sinaapp.com/config/oauth!execute.action,若报500空指针,说明填写正确。找不到方法请自行修改默认版本,找到对应的执行action即可。

原创粉丝点击