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

所属分类: 软件编程 / java 阅读数: 47
收藏 0 赞 0 分享

本文介绍了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文件中增加以下内容:

<!--钉钉工具包-->
   <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();
    } 
}
更多精彩内容其他人还在看

SpringBoot SpEL语法扫盲与查询手册的实现

这篇文章主要介绍了SpringBoot SpEL语法扫盲与查询手册的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java创建子线程的两种方法

这篇文章主要介绍了Java创建子线程的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot2.x集成JPA快速开发的示例代码

这篇文章主要介绍了Spring Boot2.x集成JPA快速开发,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

关于Java中的mysql时区问题详解

这篇文章主要给大家介绍了关于Java中mysql时区问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

JAVA参数传递方式实例浅析【按值传递与引用传递区别】

这篇文章主要介绍了JAVA参数传递方式,结合实例形式分析了java按值传递与引用传递区别及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中MessageDigest来实现数据加密的方法

这篇文章主要介绍了Java中MessageDigest来实现数据加密的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

spring 注解验证@NotNull等使用方法

这篇文章主要介绍了spring 注解验证@NotNull等使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

浅谈如何优雅地停止Spring Boot应用

这篇文章主要介绍了浅谈如何优雅地停止Spring Boot应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Python如何使用@property @x.setter及@x.deleter

这篇文章主要介绍了Python如何使用@property @x.setter及@x.deleter,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java Jmeter全局变量设置过程图解

这篇文章主要介绍了Java Jmeter全局变量设置过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多