微信获取用户openid

开发框架:struts2(零配置)html

官方文档下载地址java

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

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

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


建立WeiXinOauth2Token类。改类具备如下属性:(自行添加getset方法)
tomcat

private String accessToken;
微信

private int expiresIn;
app

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便可。

相关文章
相关标签/搜索