java 钉钉获取用户信息,JAVA maven项目如何使用钉钉SDK来获取token、用户
JAVA maven項目如何使用釘釘SDK來獲取token、用戶
發布時間:2020-07-17 10:52:32
來源:億速云
閱讀:102
作者:小豬
這篇文章主要為大家展示了JAVA maven項目如何使用釘釘SDK來獲取token、用戶,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
本文介紹了JAVA maven項目使用釘釘SDK獲取token、用戶,分享給大家,具體如下:
將SDK放一個文件里,記住文件地址。D:\eclipse-workspace\項目名\模塊名\lib
win+r cmd 敲下面的命令:
【進入相應盤】?? D:
【進入文件地址】?? cd D:\eclipse-workspace\項目名\模塊名
【運行命令】? mvn install:install-file -DgroupId=com.dingtalk.api -DartifactId=top-api-sdk-dev -Dversion=ding-open-mc-SNAPSHOT -Dfile=lib/taobao-sdk-java-auto_1479188381469-20200319.jar -Dpackaging=jar -DgeneratePom=true
然后在pom文件中增加以下內容:
com.dingtalk.api
top-api-sdk-dev
ding-open-mc-SNAPSHOT
理解:普通的maven項目都會在pom里配置好jar,項目自動從maven中配置的鏡像地址(就是網上)自己把jar包下載到你設置的位置
但是有些jar包下載不下來,可以先本地保存,然后像上面那樣用命令把jar包掛到maven設置的jar庫里去
JAVA后臺代碼:
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiUserSimplelistRequest;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiUserSimplelistResponse;
import com.taobao.api.ApiException;
import com.dingtalk.api.response.OapiDepartmentListResponse.Department;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
。。。
登錄接口方法(前端傳來code){
// 釘釘 /gettoken 獲取token,只需填寫appkey和appsecret
try {
DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest req1 = new OapiGettokenRequest();
req1.setAppkey("xxxxxxxx");
req1.setAppsecret("xxxxxxxx");
req1.setHttpMethod("GET");
OapiGettokenResponse rsp1 = client1.execute(req1);
System.out.println(rsp1.getBody());
accessToken = rsp1.getAccessToken();
} catch (Exception e) {
//} catch (ApiException e) {
e.printStackTrace();
}
// 釘釘 /user/getuserinfo 獲取用戶userid
try {
DingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
OapiUserGetuserinfoRequest req2 = new OapiUserGetuserinfoRequest();
req2.setCode(code);//
req2.setHttpMethod("GET");
OapiUserGetuserinfoResponse rsp2 = client2.execute(req2, accessToken);//
System.out.println(rsp2.getBody());
userId = rsp2.getUserid();
System.out.println("userId:" + userId);
System.out.println("------------------------------------------------------");
} catch (ApiException e) {
e.printStackTrace();
}
// 釘釘 /user/get 獲取用戶詳情
try {
DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
OapiUserGetRequest req3 = new OapiUserGetRequest();
req3.setUserid(userId);//
req3.setHttpMethod("GET");
OapiUserGetResponse rsp3 = client3.execute(req3, accessToken);//
System.out.println(rsp3.getBody());
} catch (ApiException e) {
e.printStackTrace();
}
// 釘釘 獲取所有部門列表
try {
DingTalkClient client4 = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
OapiDepartmentListRequest req4 = new OapiDepartmentListRequest();
req4.setHttpMethod("GET");
req4.setId("1");
OapiDepartmentListResponse rsp4 = client4.execute(req4, accessToken);
System.out.println("-----------------獲取所有部門列表-----------------");
System.out.println(rsp4.getBody());
List departmentList = rsp4.getDepartment();
System.out.println("-----------------------------------------------------");
// [{id: xxx, parentid: xxx, ......},{id: xxx, parentid: xxx, ......},{id: xxx, parentid: xxx, ......},{id: xxx, parentid: xxx, ......}]
System.out.println(departmentList.get(0).getId();
Long parentid = departmentList.get(0).getParentid();
} catch (ApiException e) {
e.printStackTrace();
}
}
以上就是關于JAVA maven項目如何使用釘釘SDK來獲取token、用戶的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java 钉钉获取用户信息,JAVA maven项目如何使用钉钉SDK来获取token、用户的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java转gcode,学用树莓派(8)-
- 下一篇: 防摇控制matlab,一种起重机防摇控制