c# 获取钉钉流程 详细demo
时间: 2023-06-24 09:03:50 浏览: 380
以下是使用C#获取钉钉流程的详细示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
namespace DingTalkDemo
{
class Program
{
static void Main(string[] args)
{
// 钉钉开放平台应用的AppKey
string appKey = "your app key";
// 钉钉开放平台应用的AppSecret
string appSecret = "your app secret";
// 钉钉企业ID
string corpId = "your corp id";
// 钉钉企业应用的AgentId
string agentId = "your agent id";
// 钉钉企业应用的授权码
string accessToken = GetAccessToken(appKey, appSecret, corpId, agentId);
// 获取流程实例列表
string processCode = "your process code";
string startTime = "2021-01-01 00:00:00";
string endTime = "2021-12-31 23:59:59";
int cursor = 0;
int size = 20;
OapiProcessinstanceListidsRequest req = new OapiProcessinstanceListidsRequest();
req.ProcessCode = processCode;
req.StartTime = ConvertDateTimeToInt(startTime);
req.EndTime = ConvertDateTimeToInt(endTime);
req.Cursor = cursor;
req.Size = size;
OapiProcessinstanceListidsResponse rsp = GetResponse<OapiProcessinstanceListidsResponse>(req, accessToken);
Console.WriteLine(rsp.Body);
// 获取流程实例详情
string processInstanceId = "your process instance id";
OapiProcessinstanceGetRequest req2 = new OapiProcessinstanceGetRequest();
req2.ProcessInstanceId = processInstanceId;
OapiProcessinstanceGetResponse rsp2 = GetResponse<OapiProcessinstanceGetResponse>(req2, accessToken);
Console.WriteLine(rsp2.Body);
Console.ReadKey();
}
static string GetAccessToken(string appKey, string appSecret, string corpId, string agentId)
{
IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest req = new OapiGettokenRequest();
req.Appkey = appKey;
req.Appsecret = appSecret;
req.SetHttpMethod("GET");
OapiGettokenResponse rsp = client.Execute(req);
if (rsp.Errcode != 0)
{
throw new Exception("获取access_token失败,错误码:" + rsp.Errcode + ",错误信息:" + rsp.Errmsg);
}
string accessToken = rsp.AccessToken;
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/authcorp/scopes");
OapiAuthcorpScopesRequest req2 = new OapiAuthcorpScopesRequest();
req2.MicroappAgentId = agentId;
req2.CorpId = corpId;
req2.SetHttpMethod("GET");
OapiAuthcorpScopesResponse rsp2 = GetResponse<OapiAuthcorpScopesResponse>(req2, accessToken);
if (rsp2.Errcode != 0)
{
throw new Exception("获取授权码失败,错误码:" + rsp2.Errcode + ",错误信息:" + rsp2.Errmsg);
}
string authCode = rsp2.AuthScopes.AuthUserid;
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
OapiV2UserGetuserinfoRequest req3 = new OapiV2UserGetuserinfoRequest();
req3.SetHttpMethod("GET");
OapiV2UserGetuserinfoResponse rsp3 = GetResponse<OapiV2UserGetuserinfoResponse>(req3, accessToken, authCode);
if (rsp3.Errcode != 0)
{
throw new Exception("获取用户ID失败,错误码:" + rsp3.Errcode + ",错误信息:" + rsp3.Errmsg);
}
string userId = rsp3.Result.Userid;
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
OapiV2UserGetRequest req4 = new OapiV2UserGetRequest();
req4.Userid = userId;
req4.SetHttpMethod("GET");
OapiV2UserGetResponse rsp4 = GetResponse<OapiV2UserGetResponse>(req4, accessToken);
if (rsp4.Errcode != 0)
{
throw new Exception("获取用户详情失败,错误码:" + rsp4.Errcode + ",错误信息:" + rsp4.Errmsg);
}
string accessToken2 = rsp4.Result.AuthAccessToken;
return accessToken2;
}
static T GetResponse<T>(IDingTalkRequest<T> request, string accessToken, string authCode = null) where T : DingTalkResponse
{
IDingTalkClient client = new DefaultDingTalkClient(request.GetApiName());
request.SetHttpMethod(request.GetHttpMethod());
request.SetAccessToken(accessToken);
if (!string.IsNullOrEmpty(authCode))
{
request.SetAuthCode(authCode);
}
return client.Execute(request);
}
static long ConvertDateTimeToInt(string dateTimeString)
{
DateTime dt = Convert.ToDateTime(dateTimeString);
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0));
return (long)(dt - dtStart).TotalMilliseconds;
}
}
}
```
其中,`GetAccessToken`方法用于获取访问钉钉开放平台API的`accessToken`,需要传入应用的`appKey`、`appSecret`、企业ID`corpId`和应用的`agentId`,并调用钉钉开放平台的接口获取授权码和用户ID,最终获取`accessToken`,具体实现可以参考代码。
在获取`accessToken`之后,我们可以通过调用钉钉开放平台提供的API获取流程实例列表和流程实例详情,示例代码中分别演示了如何获取流程实例列表和流程实例详情。
需要注意的是,示例代码中使用了钉钉开放平台的SDK进行API调用,需要在项目中引用相应的SDK,具体可以参考钉钉开放平台的文档。
阅读全文