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 myDDDev
10 {
11 public partial class index : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 try
16 {
17 //http://ddtalk.github.io/dingTalkDoc/
18 //獲取AccessToken
19 //開發者在調用開放平台接口前需要通過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/list
30 在請求該接口時,需要將獲取的AccessToken作為請求參數拼裝到URL中:
31 https://oapi.dingtalk.com/department/list?access_token=ACCESS_TOKEN
32 */
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 手機驗證
在手機上用釘釘客戶端查看是否創建部門成功,如下圖所示:

水平有限,望各位園友不吝賜教!如果覺得不錯,請點擊推薦和關注!
出處: http://www.cnblogs.com/isaboy/
出處: http://www.cnblogs.com/isaboy/

