微信开发之用户信息20170922
来源:互联网 发布:最优化理论 无线通信 编辑:程序博客网 时间:2024/04/29 05:04
public static JSONObject service(JSONObject params, ActionContext context) throws JsonGenerationException, JsonMappingException, IOException, WxErrorException{
/*====================写一个类来封装这段代码,begin======================*/
JSONObject map=null;
String userCode = params.getString("code");
System.out.println("code是:"+userCode);
//获取token
String id = "wxbb7c699a7f1ac509"; //要有封装类,你们框架中应该有
String Scret = "06kHjVoG9bKzMaTW0yfKQct5ttw_kmnslTfBydHTyD8";//要有封装类,你们框架中应该有
String token1=null;
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+id+"&corpsecret="+Scret;
try{
URL getUrl =new URL(url);
HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
java.io.InputStream is = http.getInputStream();
int size = is.available();
byte[] b = new byte[size];
is.read(b);
String message = new String(b, "UTF-8");
System.out.println("message"+message);
JSONObject json = JSONObject.parseObject(message);
System.out.println("json:"+json.toString());
//access_Token token = null;
// return json;
// token.获取到的token
Integer error = json.getInteger("errcode");
//1、能获取到access_token,接口无返回errcode
//2、能获取到access_token,接口返回errcode为0, errmsg为空或者"ok"
if(error==null||error==0){
//这里是获取token 没有userid 下边才有
token1= json.getString("access_token");
System.out.println("第一个token1的值是::::::"+token1);
}else{
System.out.println("获取token失败1"+json.getString("errmsg"));
return null;
}
}catch(MalformedURLException e){
System.out.println("获取token失败2");
e.printStackTrace();
}
if(StringUtils.isBlank(token1)){
System.out.println("3token1为空");
return null;
}
/*====================写一个类来封装这段代码,end======================*/
/*====================写一个类来封装这段代码,begin======================*/
try {
//获取用户信息、现在这边后台处理 code he TOKEN。
url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token="+token1+"&code="+userCode;
URL getUrl =new URL(url);
HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
//
http.connect();
java.io.InputStream is = http.getInputStream();
int size = is.available();
byte[] b = new byte[size];
is.read(b);
String message = new String(b, "UTF-8");
JSONObject json = JSONObject.parseObject(message);
System.out.println(json.toString());
//{"DeviceId":"10000505959491CO","UserId":"sunlidong","errcode":0,"errmsg":"ok"}
String UserId = json.getString("UserId");
// JSONObject map=null;
map = new JSONObject();
map.put("openid",UserId);
return map;
} catch (Exception e) {
System.out.println("获取用户信息失败");
e.printStackTrace();
return null;
}
/*====================写一个类来封装这段代码,end======================*/
//调用刚刚封装的获取token的方法,接收token参数
//判断token是否为空,为空 提示错误信息,不为空调用刚刚封装的获取userid的方法,接收JSONObject map参数
}
阅读全文
0 0
- 微信开发之用户信息20170922
- 微信开发之实现获取用户信息
- 【微信开发】获取用户信息
- php之微信开发(获取用户信息)
- java微信开发-之如何获取openid 和用户信息
- 微信公众号开发之授权获取用户信息
- 微信开发之网页授权获取用户信息
- 微信网页开发之网页授权获取用户信息
- 微信公众号开发之如何一键导出微信所有用户信息到Excel
- 微信服务号开发-获取用户信息
- 微信开发-网页授权获取用户信息
- 微信开发页面授权获取用户信息
- 微信开发接入获取用户信息
- 微信开发 获取用户信息两个接口
- 微信网页开发--获取微信用户信息
- 微信开发之获取OAuth2.0网页授权认证和获取用户信息进行关联
- 微信公众号开发之订阅号如何获取用户信息(一)
- 微信获取用户信息
- 有关于深度优先搜索的二三事儿(《算法设计与实现》摘要)
- 使用Docker镜像和仓库
- linux cifs自动挂载windows硬盘或文件夹
- XACML知识初识
- Python GUI 菜单
- 微信开发之用户信息20170922
- TI培训
- JENKIN Android Gradle 编译 出错点
- XML SAX解析xml文件
- java.net.UnknownHostException:user
- 多表查询,获取另一个表中的字段名称,作为此表的字段名称。
- PCB画板之旅------一
- MQ FTE
- (一)整合spring cloud云服务架构