服务端对接钉钉三步走
第一步:获取业务对接的appkey和appsecret
登录开发者后台,点击“应用开发”-“企业内部应用”-找到应用并点击应用图标-点击“查看详情”,可以查看AppKey和AppSecret。
第二步:根据appkey和appsecret获取登录access_token
/**
* 初始化获取 access_token
* @return
*/
public static String getAccessToken(){
String accessToken ="";
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(appkey);
request.setAppsecret(appsecret);
request.setHttpMethod("GET");
try {
OapiGettokenResponse response = client.execute(request);
accessToken = response.getAccessToken();
Long errcode = response.getErrcode();
if(null!=errcode && errcode==0l){
return accessToken;
}
} catch (ApiException e) {
e.printStackTrace();
}
return accessToken;
}
第三步:根据业务需求请求钉钉提供的服务端API接口
/**
* 获取用户名称
* @param userId
* @return
*/
public static String getUserName(String userId){
String accessToken =getAccessToken();
String userName= "";
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
OapiUserGetRequest request = new OapiUserGetRequest();
request.setUserid(userId);
request.setHttpMethod("GET");
try {
OapiUserGetResponse response = client.execute(request, accessToken);
userName = response.getName();
} catch (ApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return userName;
}
总结:根据官方提供的接口API,随机应变获取所需要的信息
提供钉钉服务端API文档地址:https://ding-doc.dingtalk.com/doc#/serverapi2/ege851
来源:oschina
链接:https://my.oschina.net/u/3204029/blog/4282454