你的位置:首页 > ASP.net教程

[ASP.net教程]C#如何在钉钉开发平台中创建部门


  钉钉是阿里巴巴专为中小企业和团队打造的沟通、协同的多端平台,钉钉开放平台旨在为企业提供更为丰富的办公协同解决方案。通过钉钉开放平台,企业或第三方合作伙伴可以帮助企业快速、低成本的实现高质量的移动微应用,实现生产、管理、协作、运营的移动化。官网的列子往往都是java,php和nodejs的,下面我用c#来实现一个简单的列子,来说明如何与钉钉进行交互:

  1 创建一个网站:

  

  其中官网有如何进行账号注册等开发的准备工作,这里不阐述。假设你已经成功申请了企业账号,然后获取了CorpId和CorpSecret。这里我创建一个DDConfig类来保存这两个值.

  2 编写DDHelper类

DDHelper类主要为了与钉钉API进行交互,实现Get和Post请求处理,代码如下:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.IO; 6 using HttpRequest; 7 namespace myDDDev 8 { 9   public static class DDHelper 10   { 11     public static string GetAccessToken(HttpRequest.HttpHelper.HttpResult result) 12     { 13       14       if (result!=null) 15       { 16         StreamReader myStreamReader = new StreamReader(result.Result, System.Text.Encoding.GetEncoding("utf-8")); 17         string retString = myStreamReader.ReadToEnd(); 18  19         M_AccessToken oat = Newtonsoft.Json.JsonConvert.DeserializeObject< M_AccessToken>(retString); 20  21         if (oat!=null) 22         { 23          if(oat.errcode==0) 24           { 25             return oat.access_token; 26           } 27         } 28       } 29       return ""; 30     } 31     public static string GetAccessToken(string url) 32     { 33  34       if (url != "") 35       { 36         try 37         { 38           HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Get(url); 39           M_AccessToken oat = Newtonsoft.Json.JsonConvert.DeserializeObject<M_AccessToken>(result.ToStringResult()); 40  41           if (oat != null) 42           { 43             if (oat.errcode == 0) 44             { 45               return oat.access_token; 46             } 47           } 48         } 49         catch(Exception ex) 50         { 51           throw; 52         } 53       } 54       return ""; 55     } 56  57     public static string CreateDept(string url,string param) 58     { 59  60       if (url != "") 61       { 62         try 63         { 64           HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Post(url, param, "application/json"); 65           DDResult oat = Newtonsoft.Json.JsonConvert.DeserializeObject<DDResult>(result.ToStringResult()); 66  67           if (oat != null) 68           { 69             if (oat.errcode == 0) 70             { 71               return "0"; 72             } 73           } 74         } 75         catch (Exception ex) 76         { 77           throw; 78         } 79       } 80       return "1"; 81     } 82  83  84  85   } 86   //{"access_token":"","errcode":0,"errmsg":"ok"} 87   public class M_AccessToken 88   { 89     public string access_token { get; set; } 90     public int errcode { get; set; } 91  92     public string errmsg { get; set; } 93  94  95   } 96   public class DDResult 97   { 98     99     public int errcode { get; set; }100     public string errmsg { get; set; }101 102 103   }104 }

  3 创建部门处理

在index.aspx.cs中编写创建部门的逻辑,代码如下:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.IO; 8 using HttpRequest; 9 namespace myDDDev10 {11   public partial class index : System.Web.UI.Page12   {13     protected void Page_Load(object sender, EventArgs e)14     {15       try16       {17         //http://ddtalk.github.io/dingTalkDoc/18         //获取AccessToken19         //开发者在调用开放平台接口前需要通过CorpID和CorpSecret获取AccessToken。20         //获取AccessToken的方法是向 https://oapi.dingtalk.com/gettoken?corpid=id&corpsecret=secrect GET请求。21         string getUrl = string.Format("https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}", DDConfig.__CorpID, DDConfig.__CorpSecret);22 23         //access_token 会失效,需要定期获取;24         string access_token = DDHelper.GetAccessToken(getUrl);25 26         /*27         开发者获取AccessToken后便可以调用开放平台其他接口。28         以获取部门列表接口为例,获取部门列表接口为:29         oapi.dingtalk.com/department/list30         在请求该接口时,需要将获取的AccessToken作为请求参数拼装到URL中:31         https://oapi.dingtalk.com/department/list?access_token=ACCESS_TOKEN32         */33         //添加部门测试34         string postUrl = string.Format("https://oapi.dingtalk.com/department/create?access_token={0}", access_token);35         string param = "{\"access_token\":\"" + access_token + "\",\"name\":\"后勤部\",\"parentid\":\"1\",\"order\":\"3\",\"createDeptGroup\":\"false\"}";36         //HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Post(postUrl, param, "application/json");37         //Response.Write(result.ToStringResult());38         //code=0 表示创建从成功39         string code = DDHelper.CreateDept(postUrl, param);40         if(code=="0")41         {42           Response.Write("创建成功");43         }44       }45       catch(Exception ex)46       {47         Response.Write(ex.Message);48       }49     }50   }51 }52 

  

 4 手机验证

 在手机上用钉钉客户端查看是否创建部门成功,如下图所示: