话不多说,直接贴代码,微信企业号端发送带参的请求 到后台找到下面方法(参数code)。
如下主流程:
—————————————————————————————————–
@RequestMapping(“/getUserWXInfo”)
public AjaxResponse getUserWXInfo(String code){
JSONObject obj = null;
String accessToken = this.getAccess_token();
String userTicket = this.getUserTicket(accessToken, code);
String responseContext = null;
if(null !=userTicket&&!””.equals(userTicket)){
responseContext = this.getUserInfoByTicket(userTicket, accessToken);
}
obj = JSON.parseObject(responseContext);
return AjaxResponse.success(“OK”,responseContext);
}
———————————————————————————————————-
主流程解析:
=======================================================================================
主要3部分:
1.getAccess_token() 方法
//获得access_token,根据企业号和企业号密钥交换(access_token一定时间内有效,因此可以在数据库缓存,避免频繁请求)
public String getAccess_token(){
String turl = “https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=”+corpid+”&corpsecret=”+corpsecret;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(turl);
String responseContent = null;
JSONObject obj = null;
try
{
HttpResponse res = client.execute(get);
// 响应内容
HttpEntity entity = res.getEntity();
responseContent = EntityUtils.toString(entity, “UTF-8”);
obj = JSON.parseObject(responseContent);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
// 关闭连接 ,释放资源
client.getConnectionManager().shutdown();
return obj.getString(“access_token”);
}
}
2.getUserTicket(String token,String code) 方法
//获得userTicket 需要access_token以及前端授权跳转的code
public String getUserTicket(String token,String code){
String turl = “https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=”+token+”&code=”+code;
System.out.println(turl);
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(turl);
String responseContent = null;
JSONObject obj = null;
try
{
HttpResponse res = client.execute(get);
// 响应内容
HttpEntity entity = res.getEntity();
responseContent = EntityUtils.toString(entity, “UTF-8”);
obj = JSON.parseObject(responseContent);
System.out.println(obj.toJSONString());
}
catch (Exception e)
{
e.printStackTrace();
}finally{
client.getConnectionManager().shutdown();
return obj.getString(“user_ticket”);
}
}
3.getUserInfoByTicket(userTicket, accessToken) 当userTicket和accessToken 都拿到后,便可以去调用该方法换取用户信息
//利用usertickt交换用户信息
public String getUserInfoByTicket(String userTicket,String token){
String uurl = “https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=”+token;
StringBuffer sb = null;
try {
//创建连接
URL url = new URL(uurl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(“POST”);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty(“contentType”, “application/json”);
connection.setRequestProperty(“Charset”, “UTF-8”);
connection.connect();
//POST请求
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
JSONObject obj = new JSONObject();
obj.put(“user_ticket”, userTicket);
out.writeBytes(obj.toString());
out.flush();
out.close();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
sb = new StringBuffer(“”);
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), “utf-8”);
sb.append(lines);
}
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new String(sb);
}
========================================================================================
——-完
今天的文章企业微信 java sdk_微信Java通用版分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/88639.html