钉钉扫码登录 java

Posted 鲲鹏展翅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了钉钉扫码登录 java相关的知识,希望对你有一定的参考价值。

 java获取扫码登录后的钉钉id 从而可以查出员工具体信息 部门具体信息

1.注册钉钉企业账号

注册地址

https://oa.dingtalk.com/register.html

开发文档地址

https://open-doc.dingtalk.com/microapp/isv

注册完支行查看corpId corpSecret

创建扫码登录使用的app ID、appSecret

 

 

2.下载钉钉sdk (http://img.alicdn.com/top/i1/LB1l7mXv0knBKNjSZKPXXX6OFXa)把jar包传到私有maven仓库

<dependency>
<groupId>com.taobao</groupId>
<artifactId>taobao-sdk-java-auto</artifactId>
<version>20180815</version>
</dependency>

(仅供参考)

 3.java代码使用示例(redis工具类没贴出来)

需导入包

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;

 


  private final String SDINGTALKSERVICE="https://oapi.dingtalk.com"; private final String METHOD_GET="GET"; /** * 先从缓存查,再到钉钉查 * @param corpId * @param corpSecret * @return */ public String getToken(String corpId,String corpSecret){ String access_token=RedisUtils.get("ACCESS_TOKEN"); if(StringUtils.isEmpty(access_token)){ access_token=getTokenFromDing(corpId,corpSecret); } return access_token; } /** * 企业内部开发获取access_token * @param corpId 企业Id * @param corpSecret 企业应用的凭证密钥 * @return */ public String getTokenFromDing(String corpId,String corpSecret){ OapiGettokenResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE+"/gettoken"); OapiGettokenRequest request = new OapiGettokenRequest(); request.setCorpid(corpId); request.setCorpsecret(corpSecret); request.setHttpMethod(METHOD_GET); response = client.execute(request); } catch (ApiException e) { e.printStackTrace(); } String body=response.getBody(); JSONObject jo = JSONObject.parseObject(body); String errcode=jo.getString("errcode"); String access_token=null; if("0".equals(errcode)){ access_token=(String) jo.get("access_token"); RedisUtils.set("ACCESS_TOKEN", access_token, 3600); } return access_token; } public String getSnsToken(String appId, String appSecret){ String access_token=RedisUtils.get("SNS_ACCESS_TOKEN"); if(StringUtils.isEmpty(access_token)){ access_token=getOpenToken(appId,appSecret); } return access_token; } /** * 获取钉钉开放应用的ACCESS_TOKEN * @param appId * @param appSecret * @return */ public String getOpenToken(String appId, String appSecret) { OapiSnsGettokenResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE+"/sns/gettoken"); OapiSnsGettokenRequest request = new OapiSnsGettokenRequest(); request.setAppid(appId); request.setAppsecret(appSecret); request.setHttpMethod(METHOD_GET); response = client.execute(request); } catch (ApiException e) { e.printStackTrace(); } String body=response.getBody(); JSONObject jo = JSONObject.parseObject(body); String errcode=jo.getString("errcode"); String access_token=null; if("0".equals(errcode)){ access_token=(String) jo.get("access_token"); RedisUtils.set("SNS_ACCESS_TOKEN", access_token, 3600); } return access_token; } /** * 获取用户授权的持久授权码 * @param accessToken * @return */ public String get_persistent_code(String accessToken,String code){ OapiSnsGetPersistentCodeResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE+"/sns/get_persistent_code"); OapiSnsGetPersistentCodeRequest request = new OapiSnsGetPersistentCodeRequest(); request.setTmpAuthCode(code); response= client.execute(request,accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); } /** * 获取用户授权的SNS_TOKEN * @param openId * @param persistentCode * @param accessToken 开放应用的token * @return */ public String get_sns_token(String openId, String persistentCode,String accessToken) { OapiSnsGetSnsTokenResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/sns/get_sns_token"); OapiSnsGetSnsTokenRequest request = new OapiSnsGetSnsTokenRequest(); request.setOpenid(openId); request.setPersistentCode(persistentCode); response = client.execute(request, accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getSnsToken(); } /** * 获取用户授权的个人信息 * @param snsToken * @return */ public String get_sns_userinfo_unionid(String snsToken) { OapiSnsGetuserinfoResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/sns/getuserinfo"); OapiSnsGetuserinfoRequest request = new OapiSnsGetuserinfoRequest(); request.setSnsToken(snsToken); request.setHttpMethod(METHOD_GET); response = client.execute(request); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); } /** * 根据unionid获取userid * @param accessToken * @param unionid * @return */ public String getUseridByUnionid(String accessToken,String unionid) { OapiUserGetUseridByUnionidResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/user/getUseridByUnionid"); OapiUserGetUseridByUnionidRequest request = new OapiUserGetUseridByUnionidRequest(); request.setUnionid(unionid); request.setHttpMethod(METHOD_GET); response = client.execute(request, accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); } /** * 获取用户授权的个人信息 * @param accessToken * @param uid * @return */ public String getUserGetOne(String accessToken,String uid) { OapiUserGetResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/user/get"); OapiUserGetRequest request = new OapiUserGetRequest(); request.setUserid(uid); request.setHttpMethod(METHOD_GET); response = client.execute(request, accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); } /** * 获取部门详情(根据部门id查询) * @param accessToken * @param deptid * @return */ public String getDepartmentInfo(String accessToken, String deptid) { OapiDepartmentGetResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/department/get"); OapiDepartmentGetRequest request = new OapiDepartmentGetRequest(); request.setId(deptid); request.setHttpMethod(METHOD_GET); response = client.execute(request, accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); }

 钉钉开发都非常简单 按照文档把要传的参数传过去 就可以获取想要的信息以及信息的增删改

以上是关于钉钉扫码登录 java的主要内容,如果未能解决你的问题,请参考以下文章

钉钉扫码登录网站(两种方式实现)

钉钉扫码登录中的签名算法在python中的实现

SpringBoot实现专有钉钉扫码登录

钉钉扫码登录

JavaWeb开发初步实现网站应用钉钉扫码登录

钉钉扫码登录web网站