流程以下:
前端
/** * 獲取access token 有效期2 個小時,有效期獲取,值不變,時間延長 * @return */ public static DdResult getAccessToken(String appid, String secret){ String requestUrl = "https://oapi.dingtalk.com/sns/gettoken?appid=APPID&appsecret=APPSECRET"; requestUrl = requestUrl.replace("APPID", appid).replace("APPSECRET", secret); String accessTokenResult = HttpClientUtil.doGet(requestUrl); log.info("------->> >> -------->> request ding ding access token result:{}", accessTokenResult); if (StringUtils.isNotBlank(accessTokenResult)) { return JSON.parseObject(accessTokenResult, new TypeReference<DdResult>(){}); } return new DdResult(); }
/** * 根據code與accessToken獲取用戶的持久受權碼 * @param code 臨時碼,只能使用一次 * @param accessToken 有效期,2個小時 * @return */ public static DdResult getPersistentCode(String code, String accessToken){ String requestUrl = "https://oapi.dingtalk.com/sns/get_persistent_code?access_token=" + accessToken; HashMap<String, String> params = Maps.newHashMap(); params.put("tmp_auth_code", code); String accessTokenResult = HttpClientUtil.doPostJson(requestUrl, JSON.toJSONString(params)); log.info("------->> >> -------->> ding ding persistent token result: {}", accessTokenResult); return JSON.parseObject(accessTokenResult, DdResult.class); }
/** * 獲取sns token * @param accessToken * @param openId * @param persistentCode * @return */ public static DdResult getSnsToken(String accessToken, String openId, String persistentCode) { HashMap<String, String> params = Maps.newHashMap(); params.put("openid", openId); params.put("persistent_code", persistentCode); String snsTokenRequestUrl = "https://oapi.dingtalk.com/sns/get_sns_token?access_token=" + accessToken; String result = HttpClientUtil.doPostJson(snsTokenRequestUrl, JSON.toJSONString(params)); log.info("------->> >> -------->> request ding ding sns token result:{}", result); return JSON.parseObject(result, new TypeReference<DdResult>(){}); }
public static DdResult getUserInfo(String snsToken) { String userInfoRequestUrl = "https://oapi.dingtalk.com/sns/getuserinfo?sns_token=" + snsToken; String result = HttpClientUtil.doGet(userInfoRequestUrl); log.info("------->> >> -------->> >> request ding ding sns token result:{}", result); if (StringUtils.isBlank(result)) { return null; } return JSON.parseObject(result, new TypeReference<DdResult>() {}); }
拿到用戶的信息就能夠去作登陸操做了。api