using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
namespace imgFileUpLoadClass
{
/// <summary>
/// 钉钉
/// </summary>
public class DD_Test
{
#region --定义全局变量保存CorpID和CorpSecret 注:CorpID和CorpSecret都可以从钉钉里获取,详见钉钉开发文档,地址:https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.KDt2dn&treeId=385&articleId=104980&docType=1#s3
/// <summary>
/// 定义全局变量CorpID
/// </summary>
private static string CorpID = "123123";
/// <summary>
/// 定义全局变量CorpSecret
/// </summary>
private static string CorpSecret = "eAcq99_6Rm7K9OL6ZYCPqQd2AdfoRKlKHL2LXf1KuGuVJAsd4P2VqTFLP3PJAW04";
#endregion
#region --GET方式获取部门信息
/// <summary>
/// 获取钉钉部门信息列表
/// </summary>
/// <returns></returns>
public static object GetDepartmentList()
{
//获取accesstoken
string AccessToken = Access_Token.UpdateAccessToken(CorpID, CorpSecret);
//拼接所请求的url
string DepartmentListURL = "https://oapi.dingtalk.com/department/list?access_token=" + AccessToken;
string result = string.Empty;
#region --未封装 (弃用)
//HttpWebRequest requst = WebRequest.Create(DepartmentListURL) as HttpWebRequest;
请求方式
//requst.Method = "GET";
设置连接HTTP标头
//requst.Connection = "application/x-www-form-urlencoded";
//HttpWebResponse response = requst.GetResponse() as HttpWebResponse;
//using (StreamReader Reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
//{
// result = Reader.ReadToEnd();
//}
//if(result==null)
//{
// return "返回结果为空";
//}
#endregion
result= Request_Class.Get_Request_Function(DepartmentListURL);
List<department> ldtt = new List<department>();
JavaScriptSerializer js = new JavaScriptSerializer();
GetDepartmentList gdl = js.Deserialize<GetDepartmentList>(result);
if(gdl.errcode=="0")
{
ldtt=gdl.department;
}
//result="数组个数为"+ gdl.department.Count().ToString();
return ldtt;
}
#endregion
#region --post方式创建成员
/// <summary>
/// post方式创建成员 (请求方式:WebRequest)
/// </summary>
/// <param name="userid">用户标识</param>
/// <param name="name">用户名称</param>
/// <param name="department">所属部门id列表 如有多个部门则按照 [1,111] 的数组格式的字符串</param>
/// <param name="mobile">手机号</param>
/// <param name="email">邮箱</param>
/// <returns></returns>
public static string CreateUser(string userid, string name, string department, string mobile, string email)
{
//department = "[36249678]";
//获取token
string accesstokrn= Access_Token.UpdateAccessToken(CorpID,CorpSecret);
//拼接请求url字符串
string PostUrl = string.Format("https://oapi.dingtalk.com/user/create?access_token={0}",accesstokrn);
string data = "{\"userid\":\"" + userid + "\",\"name\":\"" + name + "\",\"orderInDepts\":\"\",\"department\":" + department + ",\"position\":\"员工\",\"mobile\":\"" + mobile + "\",\"tel\":\"\",\"workPlace\":\"\",\"remark\":\"\",\"email\":\"" + email + "\",\"jobnumber\":\"\",\"isHide\":\"false\",\"isSenior\":\"false\",\"extattr\":{\"爱好\":\"旅游\",\"年龄\":\"20\"}}";
//拼接请求包结构体
#region --拼接请求的包结构体
//string data = string.Format("{{" +
// "\"userid\":\"{0}\"," + //标识
// "\"name\":\"{1}\"," + // 不可空 姓名
// "\"orderInDepts\":\"\"," + //在对应的部门中的排序 key是部门的Id, value是人员在这个部门的排序值
// "\"department\":{2}," + // 不可空 成员所属部门id列表
// "\"position\":\"员工\"," + //职位信息
// "\"mobile\":\"{3}\"," + // 不可空 手机号码,企业内必须唯一,不可重复
// "\"tel\":\"\"," + //分机号
// "\"workPlace\":\"\"," + //办公地点
// "\"remark\":\"\"," + //备注
// "\"email\":\"{4}\"," + //邮箱
// "\"jobnumber\":\"\"," + //员工工号
// "\"isHide\":\"false\"," + //是否号码隐藏
// "\"isSenior\":\"false\"," + //是否高管模式
// "\"extattr\":{{" + //扩展属性
// "\"爱好\":\"旅游\"," +
// "\"年龄\":\"20\"" +
// "}}" +
// "}}", userid, name, department, mobile, email);
#endregion
#region --拼接请求的包结构体
//string data ="{userid:"+userid+",name:{1},orderInDepts:,department:"+department+",position:,mobile:"+mobile+",tel:,workPlace:,remark:,email:"+email+",jobnumber:,isHide:false,isSenior:false,extattr:{爱好:旅游,年龄:20}}";
#endregion
return Request_Class.Post_Request_Function(PostUrl, data);
//return data;
}
#endregion
#region --get方式获取管理员列表
/// <summary>
/// 获取管理员列表
/// </summary>
public static string GetAdministrators()
{
string AccessToken = Access_Token.UpdateAccessToken(CorpID, CorpSecret);
string url = "https://oapi.dingtalk.com/user/get_admin?access_token=" + AccessToken;
return Request_Class.Get_Request_Function(url);
}
#endregion
#region --get方式获取用户信息
/// <summary>
/// 获取用户信息
/// </summary>
public static string GetUserInfo(string userid)
{
string AccessToken = Access_Token.UpdateAccessToken(CorpID, CorpSecret);
string url = "https://oapi.dingtalk.com/user/get?access_token="+ AccessToken + "&userid="+ userid;
return Request_Class.Get_Request_Function(url);
}
#endregion
#region --get方式获取部门成员
/// <summary>
/// 获取部门成员
/// </summary>
/// <param name="department_id">部门id</param>
/// <returns></returns>
public static string GetSimplelistUserInfo(string department_id)
{
string AccessToken = Access_Token.UpdateAccessToken(CorpID, CorpSecret);
string url = "https://oapi.dingtalk.com/user/list?access_token="+ AccessToken + "&department_id=" + department_id;
string response = string.Empty;
response= Request_Class.Get_Request_Function(url);
//UserList UserList = new UserList();
//if (!string.IsNullOrEmpty(response))
//{
// JavaScriptSerializer js = new JavaScriptSerializer();
// GetUser gdl = js.Deserialize<GetUser>(response);
// if (gdl.UserList == null)
// {
// UserList = gdl.UserList;
// }
//}
return response;
}
#endregion
}
/// <summary>
/// 获取钉钉accesstoken
/// </summary>
public class Access_Token
{
#region --获取钉钉的AccessToken
/// <summary>
/// 获取AccessToken
/// </summary>
/// <param name="CorpID">钉钉的唯一标识CorpID</param>
/// <param name="CorpSecret">标识对应的秘钥</param>
/// <returns></returns>
public static string UpdateAccessToken(string CorpID, string CorpSecret)
{
//开发者在调用开放平台接口前需要通过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}", CorpID, CorpSecret);
//access_token 会失效,需要定期获取;
#region --请求钉钉 获取AccessToken信息 (弃用)
//HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Get(getUrl);
//var oat = Newtonsoft.Json.JsonConvert.DeserializeObject<M_AccessToken>(result.ToStringResult());
#endregion
#region --根据CorpID、CorpSecret获取钉钉AccessToken
var request = WebRequest.Create(getUrl) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
string result = string.Empty;
HttpWebResponse response = null;
response = request.GetResponse() as HttpWebResponse;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
}
if (result == null)
{
return "返回值为null";
}
#endregion
JavaScriptSerializer js = new JavaScriptSerializer();
JsonEntity sn = js.Deserialize<JsonEntity>(result);
if(sn.errcode != 0)
{
result = sn.access_token;
}
return sn.access_token;
}
#endregion
}
/// <summary>
/// 封装两种底层请求方式主体 请求方式:WebRequest
/// </summary>
public class Request_Class
{
/// <summary>
///post请求方式请求API 请求主体
/// </summary>
/// <param name="url">请求的url</param>
/// <param name="data">请求参数</param>
/// <param name="referer">referer HTTP 标头 (可选)</param>
/// <returns></returns>
public static string Post_Request_Function(string url,string data, params string[] referer)
{
string retString = string.Empty;
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "post"; //请求方式
//request.Referer = ""; //Referer 标头
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
byte[] bytes = Encoding.UTF8.GetBytes(data);
request.ContentLength = bytes.Length;
request.GetRequestStream().Write(bytes, 0, bytes.Length);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
retString = reader.ReadToEnd();
}
}
catch (Exception e)
{
retString = e.Message;
}
if(string.IsNullOrEmpty(retString))
{
return "返回值为空";
}
return retString;
}
/// <summary>
/// get方式请求API 请求主体
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string Get_Request_Function(string url)
{
string retString = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//请求方式
request.Method = "GET";
//Accept HTTP标头
request.Accept = "text/html, application/xhtml+xml, */*";
//ContentType标头
request.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
retString = reader.ReadToEnd();
}
}
catch (Exception e)
{
retString = e.Message;
}
if(string.IsNullOrEmpty(retString))
{
return "返回值为空";
}
return retString;
}
}
#region --实体
#region --Access_Token实体
/// <summary>
/// Access_Token实体
/// </summary>
public class JsonEntity
{
/// <summary>
/// 错误码
/// </summary>
public int errcode { get; set; }
/// <summary>
/// 获取到的凭证
/// </summary>
public string access_token { get; set; }
/// <summary>
/// 错误信息
/// </summary>
public string errmsg { get; set; }
/// <summary>
/// 过期时间
/// </summary>
public int expires_in { get; set; }
}
#endregion
#region --用户实体
/// <summary>
/// 获取用户信息
/// </summary>
public class GetUser
{
/// <summary>
/// 返回码
/// </summary>
public string errcode { get; set; }
/// <summary>
/// 对返回码的文本描述内容
/// </summary>
public string errmsg { get; set; }
/// <summary>
/// 在分页查询时返回,代表是否还有下一页更多数据
/// </summary>
public string hasMore { get; set; }
/// <summary>
/// 用户列表
/// </summary>
public UserList UserList { get; set; }
}
/// <summary>
/// 用户信息
/// </summary>
public class UserList
{
/// <summary>
/// 员工唯一标识ID(不可修改)
/// </summary>
public string userid { get; set; }
/// <summary>
/// 成员名称
/// </summary>
public string name { get; set; }
/// <summary>
/// 分机号(仅限企业内部开发调用)
/// </summary>
public string tel { get; set; }
/// <summary>
/// 办公地点(ISV不可见)
/// </summary>
public string workPlace { get; set; }
/// <summary>
/// 备注(ISV不可见)
/// </summary>
public string remark { get; set; }
/// <summary>
/// 手机号码(ISV不可见)
/// </summary>
public string mobile { get; set; }
/// <summary>
/// 员工的电子邮箱(ISV不可见)
/// </summary>
public string email { get; set; }
/// <summary>
/// 员工的企业邮箱(ISV不可见)
/// </summary>
public string orgEmail { get; set; }
/// <summary>
/// 是否已经激活, true表示已激活, false表示未激活
/// </summary>
public string active { get; set; }
/// <summary>
/// 在对应的部门中的排序,
/// Map结构的json字符串,
/// key是部门的Id, value是人员在这个部门的排序值
/// </summary>
public string orderInDepts { get; set; }
/// <summary>
/// 是否为企业的管理员
/// true表示是, false表示不是
/// </summary>
public string isAdmin { get; set; }
/// <summary>
/// 是否为企业的老板
/// true表示是, false表示不是
/// </summary>
public string isBoss { get; set; }
/// <summary>
/// 钉钉Id,在钉钉全局范围内标识用户的身份(不可修改)。
/// </summary>
public string dingId { get; set; }
/// <summary>
/// 在当前isv全局范围内唯一标识一个用户的身份,用户无法修改
/// </summary>
public string unionid { get; set; }
/// <summary>
/// 在对应的部门中是否为主管
/// Map结构的json字符串
/// key是部门的Id, value是人员在这个部门中是否为主管, true表示是, false表示不是
/// </summary>
public string isLeaderInDepts { get; set; }
/// <summary>
/// 是否号码隐藏, true表示隐藏, false表示不隐藏
/// </summary>
public string isHide { get; set; }
/// <summary>
/// 成员所属部门id列表
/// </summary>
public string department { get; set; }
/// <summary>
/// 职位信息
/// </summary>
public string position { get; set; }
/// <summary>
/// 头像url
/// </summary>
public string avatar { get; set; }
/// <summary>
/// 入职时间
/// </summary>
public string hiredDate { get; set; }
/// <summary>
/// 员工工号
/// </summary>
public string jobnumber { get; set; }
/// <summary>
/// 扩展属性
/// </summary>
public string extattr { get; set; }
/// <summary>
/// 角色信息(ISV不可见),json数组格式
/// </summary>
public List<roles> roles { get; set; }
}
/// <summary>
/// 角色信息
/// </summary>
public class roles
{
/// <summary>
/// 角色id(ISV不可见)
/// </summary>
public int id { get; set; }
/// <summary>
/// 角色名称(ISV不可见)
/// </summary>
public int name { get; set; }
/// <summary>
/// 角色分组名称(ISV不可见)
/// </summary>
public int groupName { get; set; }
}
#endregion
#region --部门实体
public class GetDepartmentList
{
/// <summary>
/// 返回码
/// </summary>
public string errcode { get; set; }
/// <summary>
/// 对返回码的文本描述内容
/// </summary>
public string errmsg { get; set; }
/// <summary>
/// 部门列表数据。以部门的order字段从小到大排列
/// </summary>
public List<department> department { get; set; }
}
/// <summary>
/// 部门列表数据
/// </summary>
public class department
{
/// <summary>
/// 部门id
/// </summary>
public string id { get; set; }
/// <summary>
/// 对返回码的文本描述内容
/// </summary>
public string errcode { get; set; }
/// <summary>
/// 部门名称
/// </summary>
public string name { get; set; }
/// <summary>
/// 父部门id,根部门为1
/// </summary>
public string parentid { get; set; }
/// <summary>
/// 是否同步创建一个关联此部门的企业群
/// true表示是, false表示不是
/// </summary>
public string createDeptGroup { get; set; }
/// <summary>
/// 当群已经创建后,是否有新人加入部门会自动加入该群
/// true表示是, false表示不是
/// </summary>
public string autoAddUser { get; set; }
}
#endregion
#endregion
}
c#对钉钉的部门/成员的各项操作
最新推荐文章于 2024-07-22 10:33:42 发布