import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
//模拟浏览器发送http请求完成固定地址的请求
public class QyWxUtils {
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
//获取企业微信的企业号,根据不同企业更改
private final static String CORPID = "你的企业id";
//获取企业应用的密钥,根据不同应用更改
private final static String CORPSECRET = "你的企业密匙";
//获取访问权限码URL 接口是固定的
private final static String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
// 调用手机号调用账号接口 是固定的
private final static String GET_URSRID_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=";
// 根据手机号 得到用户账号
public static String GetUserID(String mobile,String token){
String url=GET_URSRID_URL + token;
JSONObject json = new JSONObject();
json.put("mobile", mobile);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.fromObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return (String) response.get("userid");
}
// 获取token
public static String GetToken(String corpid,String corpsecret) {
Map<String, String> header = new HashMap<>();
header.put("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
String url=ACCESS_TOKEN_URL + "?corpid=" + corpid + "&corpsecret=" + corpsecret;
HttpGet httpGet = new HttpGet(url);
//设置头部
for(Map.Entry entry:header.entrySet()){
httpGet.setHeader(entry.getKey().toString(),entry.getValue().toString());
}
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpGet);
} catch (IOException e1) {
e1.printStackTrace();
}
String result = null;
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} catch (ParseException | IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
JSONObject jsonObject = JSONObject.fromObject(result);
String access_token = (String) jsonObject.get("access_token");
return access_token;
}
public static void main(String[] args) throws Exception {
String token = GetToken(CORPID, CORPSECRET);
String userID = GetUserID("你的手机号", token);
System.out.println("userID = " + userID);
}
}
java实现,企业微信获取token然后根据手机号获取用户id
于 2023-12-06 15:03:57 首次发布