钉钉企业自建应用 获取用户信息

684 阅读2分钟

在使用钉钉管理企业时,我们可以在钉钉的开放平台注册自建的企业微应用,下面为大家总结下获取用户信息的一些方式.

1.创建企业内应用

创建企业内应用,在此不再多家赘述,请参考官方文档

2.获取用户信息的流程

开发流程详情可在官网查看

开发流程
通过流程我们可以使用两种方式获取用户信息

  • 1.获取accessToken-->jsTicket-->前端配置dd.config-->前端获取authCode-->获取userId-->获取userInfo
  • 2.前端获取authCode-->后端获取userId-->获取userInfo 比较两种方式,第一种方式是完全鉴权的方式,适用于对用户信息请求次数较多的场景,如果对用户信息获取次数较少可以使用第二种,免鉴权的方式去请求用户信息

3.具体实现

此处我分享的是第二种场景,对用户信息请求较少的情况,如需实现完全鉴权请参考流程及官网demo进行实现

首先获取accessToken

     * get Ding Ding access token
     *
     * @return
     * @throws Exception
     */
    public static String getAccessToken(String corpid, String corpsecret) throws Exception {
        String url = "https://oapi.dingtalk.com/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
        JSONObject response = HttpHelper.httpGet(url);
        String accessToken;
        if (response.containsKey("access_token")) {
            accessToken = response.getString("access_token");
        } else {
            throw new Exception();
        }
        return accessToken;

    }

前端获取code在此不再赘述,请参考官方文档,通过前端获取的code和accessToken获取userId,官网描述.

     * get Ding Ding user id
     *
     * @return
     * @throws Exception
     */
    public static String getUserId(String code, String accessToken) throws Exception {
        String url = "https://oapi.dingtalk.com/user/getuserinfo?access_token=" + accessToken + "&code=" + code;
        JSONObject response = HttpHelper.httpGet(url);
        String userId;
        if (response.containsKey("userid")) {
            userId = response.getString("userid");
        } else {
            throw new Exception();
        }
        return userId;

    }

通过userId获取用户信息,官网描述

 /**
     * get Ding Ding user info
     *
     * @return
     * @throws Exception
     */
    public static JSONObject getUserInfo(String userId, String accessToken) throws Exception {
        String url = "https://oapi.dingtalk.com/user/get?userid=" + userId + "&access_token=" + accessToken;
        JSONObject response = HttpHelper.httpGet(url);
        JSONObject myObject;
        if (response.containsKey("name")) {
            myObject = new JSONObject(response);
        } else {
            throw new Exception();
        }
        return myObject;

    }

HttpHelper我使用的是官网demo中的get请求

public class HttpHelper {

    public static JSONObject httpGet(String url) throws DingDingApiException {
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().
                setSocketTimeout(2000).setConnectTimeout(2000).build();
        httpGet.setConfig(requestConfig);

        try {
            response = httpClient.execute(httpGet, new BasicHttpContext());

            if (response.getStatusLine().getStatusCode() != 200) {

                System.out.println("request url failed, http code=" + response.getStatusLine().getStatusCode()
                        + ", url=" + url);
                return null;
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String resultStr = EntityUtils.toString(entity, "utf-8");

                JSONObject result = JSON.parseObject(resultStr);
                if (result.getInteger("errcode") == 0) {
                    return result;
                } else {
                    System.out.println("request url=" + url + ",return value=");
                    System.out.println(resultStr);
                    int errCode = result.getInteger("errcode");
                    String errMsg = result.getString("errmsg");
                    throw new DingDingApiException(errCode, errMsg);
                }
            }
        } catch (IOException e) {
            System.out.println("request url=" + url + ", exception, msg=" + e.getMessage());
            e.printStackTrace();
        } finally {
            if (response != null) try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}

至此我们就可以将获取的用户信息返回给前端使用,对于用户信息时效性要求较低的场景,我们可以将其保存到cookies中,下次直接使用。