C#如何在钉钉开发平台中创建部门

2022-12-09,,,,

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

  1 创建一个网站:

  

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

2 编写DDHelper类

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using HttpRequest;
namespace myDDDev
{
public static class DDHelper
{
public static string GetAccessToken(HttpRequest.HttpHelper.HttpResult result)
{ if (result!=null)
{
StreamReader myStreamReader = new StreamReader(result.Result, System.Text.Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd(); M_AccessToken oat = Newtonsoft.Json.JsonConvert.DeserializeObject< M_AccessToken>(retString); if (oat!=null)
{
if(oat.errcode==)
{
return oat.access_token;
}
}
}
return "";
}
public static string GetAccessToken(string url)
{ if (url != "")
{
try
{
HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Get(url);
M_AccessToken oat = Newtonsoft.Json.JsonConvert.DeserializeObject<M_AccessToken>(result.ToStringResult()); if (oat != null)
{
if (oat.errcode == )
{
return oat.access_token;
}
}
}
catch(Exception ex)
{
throw;
}
}
return "";
} public static string CreateDept(string url,string param)
{ if (url != "")
{
try
{
HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Post(url, param, "application/json");
DDResult oat = Newtonsoft.Json.JsonConvert.DeserializeObject<DDResult>(result.ToStringResult()); if (oat != null)
{
if (oat.errcode == )
{
return "";
}
}
}
catch (Exception ex)
{
throw;
}
}
return "";
} }
//{"access_token":"","errcode":0,"errmsg":"ok"}
public class M_AccessToken
{
public string access_token { get; set; }
public int errcode { get; set; } public string errmsg { get; set; } }
public class DDResult
{ public int errcode { get; set; }
public string errmsg { get; set; } }
}

3 创建部门处理

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HttpRequest;
namespace myDDDev
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
//http://ddtalk.github.io/dingTalkDoc/
//获取AccessToken
//开发者在调用开放平台接口前需要通过CorpID和CorpSecret获取AccessToken。
//获取AccessToken的方法是向 https://oapi.dingtalk.com/gettoken?corpid=id&corpsecret=secrect GET请求。
string getUrl = string.Format("https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}", DDConfig.__CorpID, DDConfig.__CorpSecret); //access_token 会失效,需要定期获取;
string access_token = DDHelper.GetAccessToken(getUrl); /*
开发者获取AccessToken后便可以调用开放平台其他接口。
以获取部门列表接口为例,获取部门列表接口为:
oapi.dingtalk.com/department/list
在请求该接口时,需要将获取的AccessToken作为请求参数拼装到URL中:
https://oapi.dingtalk.com/department/list?access_token=ACCESS_TOKEN
*/
//添加部门测试
string postUrl = string.Format("https://oapi.dingtalk.com/department/create?access_token={0}", access_token);
string param = "{\"access_token\":\"" + access_token + "\",\"name\":\"后勤部\",\"parentid\":\"1\",\"order\":\"3\",\"createDeptGroup\":\"false\"}";
//HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Post(postUrl, param, "application/json");
//Response.Write(result.ToStringResult());
//code=0 表示创建从成功
string code = DDHelper.CreateDept(postUrl, param);
if(code=="")
{
Response.Write("创建成功");
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
}

4 手机验证

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

C#如何在钉钉开发平台中创建部门的相关教程结束。

《C#如何在钉钉开发平台中创建部门.doc》

下载本文的Word格式文档,以方便收藏与打印。