.Net 集成钉钉
接通知要做钉钉组件开发,一头雾水。慢慢研究。发现是官方提供的接口供调用,该怎么写成组件呢。查了好多教程和大家的经验,很详细也很有用。根据自己这几天的摸索写下经验。
但是为啥要接钉钉呢?使用钉钉的平台,钉钉的内部一些功能,用户数据角色管理审批管理等,无需关注待办工作流内部流程状态同步及处理,无需关注工作流模板创建及维护;只需关注产品业务场景,同时还可以节省企业开发成本。接入钉钉使用钉钉的一些功能,不用在重复开发。钉钉提供了丰富的api供调用,实现企业系统与钉钉的集成打通。
钉钉中的准备:
注册钉钉企业账户(https://oa.dingtalk.com/#/login)-》钉钉开放平台上创建应用-》授权一个开发人员,并获取CorpSecret,需要把corpId和CorpSecret作为参数请求api接口获取AccessToken,后面的所有接口都需要AccessToken。
下面的图是网上盗的,详细查看https://www.cnblogs.com/westsoft/p/11282057.html
完成以后就可以开始写代码了。
1.根据之前获取的corpId和CorpSecret获取token
private OapiGettokenResponse GetAccessToken(string CorpId, string CorpSecret, long AgentID) { IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken"); OapiGettokenRequest request = new OapiGettokenRequest(); request.Corpid = CorpId; request.Corpsecret = CorpSecret; request.SetHttpMethod("GET"); OapiGettokenResponse response = client.Execute(request); if (response.Errcode == 0) { LogHelper.WriteLog(response.ErrMsg); return response; } else { return null; } }
2.获取用户信息
/// <summary> /// 获取用户信息 /// </summary> /// <param name="CorpId"></param> /// <param name="CorpSecret"></param> /// <param name="AgentID"></param> /// <param name="Userid"></param> /// <returns></returns> public OapiUserGetResponse Getuserinfo(string CorpId, string CorpSecret, long AgentID,string Userid) { IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get"); OapiUserGetRequest request = new OapiUserGetRequest(); request.Userid = Userid; //员工id request.SetHttpMethod("GET"); var accesstoken = GetAccessToken(CorpId, CorpSecret, AgentID).AccessToken; if (accesstoken == null) { return null; } else { OapiUserGetResponse response = client.Execute(request, accesstoken); LogHelper.WriteLog(response.Body); return response; } } /// <summary> /// 获取当前部门下的userid列表 /// </summary> /// <param name="CorpId"></param> /// <param name="CorpSecret"></param> /// <param name="AgentID"></param> /// <param name="DeptId"></param> /// <returns></returns> public OapiUserGetDeptMemberResponse GetuserBydept(string CorpId, string CorpSecret, long AgentID, string DeptId) { IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember"); OapiUserGetDeptMemberRequest req = new OapiUserGetDeptMemberRequest(); req.DeptId= DeptId; req.SetHttpMethod("GET"); var accesstoken = GetAccessToken(CorpId, CorpSecret, AgentID).AccessToken; if (accesstoken == null) { return null; } else { OapiUserGetDeptMemberResponse rsp = client.Execute(req, accesstoken); LogHelper.WriteLog(rsp.Body); return rsp; } }
3.获取审批
public OapiProcessinstanceGetResponse CreateFlow(string CorpId, string CorpSecret, long AgentID, string ProcessInstanceId) { IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get"); OapiProcessinstanceGetRequest req = new OapiProcessinstanceGetRequest(); //审批流唯一码 req.ProcessInstanceId = ProcessInstanceId; var accesstoken = GetAccessToken(CorpId, CorpSecret, AgentID).AccessToken; if (accesstoken == null) { return null; } else { OapiProcessinstanceGetResponse rsp = client.Execute(req, accesstoken); return rsp; } }
只做了几个例子。其他按接口都可以实现。
关于所有的钉钉接口都在钉钉官网内,不过用的是java但是net版类似。详细看官网
https://ding-doc.dingtalk.com/doc#/serverapi3/is3zms
https://open-dev.dingtalk.com/apiExplorer#/?devType=isv&api=/user/getuserinfo
有个与钉钉集成的案例,因为不涉及这么多功能,到时候有具体需求在研究。https://help.finereport.com/finereport8.0/doc-view-1473.html
有个问题想不通,如果使用钉钉管理企业用户,那数据是存放在哪里呢?钉钉还是第三方呢。那数据安全怎么弄呢。