欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA maven项目使用钉钉SDK获取token、用户

程序员文章站 2022-06-06 16:07:55
本文介绍了java maven项目使用钉钉sdk获取token、用户,分享给大家,具体如下:将sdk放一个文件里,记住文件地址。d:\eclipse-workspace\项目名\模块名\libwin+...

本文介绍了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

JAVA maven项目使用钉钉SDK获取token、用户

然后在pom文件中增加以下内容:

<!--钉钉工具包-->
   <dependency>
     <groupid>com.dingtalk.api</groupid>
     <artifactid>top-api-sdk-dev</artifactid>
     <version>ding-open-mc-snapshot</version>
   </dependency>

理解:普通的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<department> 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、用户的文章就介绍到这了,更多相关maven 钉钉sdk获取token、用户内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: JAVA 钉钉SDK