using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Text; namespace GuangFu.StringTracing.Common { public class DingDingApi { #region 获取钉钉Token public string GetDingDingToken() { string path = "https://api.dingtalk.com/v1.0/oauth2/accessToken"; string result = string.Empty; var body = @"{ " + "\n" + @" ""appKey"": ""dinglybp0k8xeqrb74e1"", " + "\n" + @" ""appSecret"": ""4c-zDxQdTTRTpqNzUdopD852dzChhRjeVM6oWkwUiioqza-WIL9BtUlYPubzZ7B4"" " + "\n" + @"}"; HttpContent httpContent = new StringContent(body); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; DingDingTokenData tokenData = JsonConvert.DeserializeObject(result); return tokenData.accessToken; } else { return ""; } } } #endregion #region 发送审批实例 public string ProcessInstances(string Token,string JsonData) { string path = "https://api.dingtalk.com/v1.0/workflow/processInstances"; string result = string.Empty; HttpContent httpContent = new StringContent(JsonData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("x-acs-dingtalk-access-token", Token); //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; return result; } else { string errorContent = response.Content.ReadAsStringAsync().Result; return $"请求失败。状态码: {(int)response.StatusCode},错误信息: {errorContent}"; } } } #endregion #region 获取部门列表 public string GetDepartment(string Token, string JsonData) { string path = "https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token=" + Token; string result = string.Empty; HttpContent httpContent = new StringContent(JsonData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; return result; } else { string errorContent = response.Content.ReadAsStringAsync().Result; return $"请求失败。状态码: {(int)response.StatusCode},错误信息: {errorContent}"; } } } #endregion #region 根据用户名获取钉钉用户UserId public string GetUserID(string Token, string UserName) { string path = "https://api.dingtalk.com/v1.0/contact/users/search"; string result = string.Empty; //string JsonData = "{\"queryWord\": \""+ UserName + "\",\"offset\": 0,\"size\": 10,\"fullMatchField\": 1}"; string JsonData = "{\"queryWord\": \"" + UserName + "\",\"offset\": 0,\"size\": 10}"; HttpContent httpContent = new StringContent(JsonData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("x-acs-dingtalk-access-token", Token); //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; return result; } else { string errorContent = response.Content.ReadAsStringAsync().Result; return $"请求失败。状态码: {(int)response.StatusCode},错误信息: {errorContent}"; } } } #endregion } #region 自定义变量 public class DingDingTokenData { public string expireIn; public string accessToken; } public class SearchUserID { public bool hasMore { get; set; } public long totalCount { get; set; } public List list { get; set; } } #endregion }