【移动】获取客户微信号信息

来源:互联网 发布:金蝶软件客户端安装 编辑:程序博客网 时间:2024/05/01 07:13
/** * 【方法用途】 *   微信公众平台获取用户账户信息 * 【方法说明】 *   第一次请求access_token,作为网页授权接口调用凭证,也用作后续的接口请求凭证,该凭证有效期2小时,可以无限使用, * 但仅限于本次用户授权登录绑定的CODE。若凭证过期则需要刷新凭证,本方法只作帐户登录前获取微信用户信息,不需要考虑过期问题。 *   第二次请求用于获取用户信息,需要的参数有:第一次请求返回的"access_token"和"openid"。 * 【备注】 *   在发出获取用户信息请求前,我们需要先为微信用户取得授权码:CODE。 *   此处已经在微信公众号菜单中写入授权请求,请求完毕微信接口会跳转到我们的页面,用户进入页面时就已经取得CODE(GET请求)。 * @return */public String requestPermission(){try {System.out.println("########getWeChatPermission#########");String code = httpServletRequest.getParameter("requestCode");String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"+"appid=" + WeChatUtil.APPID +"&secret=" + WeChatUtil.SECRET+"&code=" + code+"&grant_type=authorization_code";        String result = HttpRequestUtil.getRequest(url);System.out.println(result);JSONObject permission = JSONObject.fromObject(result);/* * 判断返回错误信息的话只有两个内容 * 示例:{"errcode":40029,"errmsg":"invalid code"}  */if(permission.size()>2){//网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同//String access_token = permission.getString("access_token");//access_token接口调用凭证超时时间,单位(秒)//String expires_in = permission.getString("expires_in");//用户刷新access_token//String refresh_token = permission.getString("refresh_token");

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

这里我们要用上面的这个来获取access_token,而前面我获取到的access_token没什么用,就不用管它了

String url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"    + "&appid=" + WeChatUtil.APPID    + "&secret=" + WeChatUtil.SECRET;String tokenInfo = HttpRequestUtil.getRequest(url1);JSONObject tokenResult = JSONObject.fromObject(tokenInfo);String access_token = tokenResult.getString("access_token");

//用户唯一标识String openid = permission.getString("openid");//用户授权的作用域,使用逗号(,)分隔//String scope = permission.getString("scope");String url2 = "https://api.weixin.qq.com/sns/userinfo?"+ "access_token=" + access_token+ "&openid=" + openid+ "&lang=zh_CN";String customerInfo = HttpRequestUtil.getRequest(url2);JSONObject info = JSONObject.fromObject(customerInfo);/* * 判断返回错误信息的话只有两个内容 * 示例:{"errcode":40029,"errmsg":"invalid code"}  */if(info.size()>2){//用户昵称//String nickname = info.getString("nickname");//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知//String sex = info.getString("sex");//用户个人资料填写的省份//String province = info.getString("province");//普通用户个人资料填写的城市//String city = info.getString("city");//国家,如中国为CN//String country = info.getString("country");//用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。//若用户更换头像,原有头像URL将失效。//String headimgurl = info.getString("headimgurl");//用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)//String privilege = info.getString("privilege");map = new HashMap<String, Object>();map.put("flag", "success");/* * 只有在开发人员将公众号绑定到微信开放平台帐号后,才会出现该字段。 * unionid是客户在本公众号平台上的唯一标识,是用于本平台登录的最重要参数 */map.put("unionid", info.getString("unionid"));//此处需要对微信传回的“昵称”进行转码,否则会显示乱码map.put("nickname", new String(info.getString("nickname").getBytes("ISO8859-1"), "UTF-8"));map.put("sex", info.getString("sex"));map.put("openid", openid);System.out.println("unionid:"+info.getString("unionid"));json = (JSON) JSON.toJSON(map);return "success";}else{map = new HashMap<String, Object>();map.put("flag", "error");map.put("errcode", info.getString("errcode"));json = (JSON) JSON.toJSON(map);return "error";}}else{map = new HashMap<String, Object>();map.put("flag", "error");map.put("errcode", permission.getString("errcode"));json = (JSON) JSON.toJSON(map);return "error";}} catch (Exception e) {e.printStackTrace();}return "error";}


public static String getRequest(String url) {        //构造HttpClient实例        HttpClient client = new HttpClient();        GetMethod method = null ;        //接收返回结果        String result = null;        try {        //创建GET方法的实例        method = new GetMethod(url);            //执行HTTP GET方法请求            client.executeMethod(method);            //返回处理结果            result = method.getResponseBodyAsString();                        if(result.contains("error")){            result=null;            }                    } catch (Exception e) {            // 发生网络异常            System.out.println("发生网络异常!");            e.printStackTrace();            return null;        } finally {            //释放链接          if (method != null) {          method.releaseConnection();          }            //关闭HttpClient实例            if (client != null) {                ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();                client = null;            }        }        return result;    }


原创粉丝点击