2、 获取部门id
访问地址:https://oapi.dingtalk.com/department/list
参数:访问凭证,
调用接口后,获得所有的部门信息,将部门ID和部门名称存放在数据字典中。
private Dictionary<string,string> GetDepartments(string Token,out string Message)
{
Message = null;
Dictionary<string, string> dic = new Dictionary<string, string>();
try
{
IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
OapiDepartmentListRequest req = new OapiDepartmentListRequest();
req.SetHttpMethod("GET");
OapiDepartmentListResponse rsp = client.Execute(req, Token);
JObject jo = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(rsp.Body);
string errorCode = jo["errcode"].ToString();
if (errorCode == "0")
{
JArray jaDepartment = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(jo["department"].ToString());
foreach (JObject joDept in jaDepartment)
{
if (!dic.ContainsKey(joDept["id"].ToString()))
dic.Add(joDept["id"].ToString(), joDept["name"].ToString());
}
}
else
{
Message = jo["errmsg"].ToString();
}
}
catch(Exception ex)
{
Message = ex.Message;
}
return dic;
}